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.