Python · Stage 5 – Grouping Data · Lesson 29 of 50

Adding and Removing List Items

Change a list with one clear method at a time.

tasks = ["read"]
tasks.append("practice")
Line-by-line explanation
  1. The first line creates a one-item list.
  2. tasks.append selects the list’s append method.
  3. The method adds practice to the end.
tasks.remove("read")
Line-by-line explanation
  1. remove searches for the value read.
  2. That first matching item is deleted.
  3. The list now contains only practice.

pop() removes and returns an item by index. Assignment can replace an existing item.

tasks[0] = "review"
Line-by-line explanation
  1. Index 0 selects the first item.
  2. = replaces that position.
  3. The list now contains review.

Tiny Practice

Add milk to the shopping list.

Starter template

items = ["bread"]
CHANGE_ME
print(items)
Line-by-line explanation
  1. The first line creates the list.
  2. The missing line should change it.
Hint
  1. Use append.
  2. Call it with a dot.
  3. Put milk in quotes.
Show Solution
items = ["bread"]
items.append("milk")
print(items)
Line-by-line explanation
  1. The list starts with bread.
  2. Append adds milk at the end.
  3. Print displays both items.