List operations intentionally change list state.
You already know that a list stores ordered items and that each item has an index. Now you will choose operations that deliberately change its contents: list before → operation → list after. A list is <strong>mutable</strong>, so an operation can change the existing list instead of making you start over.
tasks = ["Learn Python", "Practice loops"]
tasks.append("Build a project")
print(tasks)Read this as a state change: ["Learn Python", "Practice loops"] → append("Build a project") → ["Learn Python", "Practice loops", "Build a project"]. append(value) adds one item at the end; the earlier items keep their order, and the same list changes.
['Learn Python', 'Practice loops', 'Build a project']Ask one useful question before you choose a method: “Do I know the value, or do I know the position?” remove(value) looks for a matching value. pop(index) removes the item at a position and returns that removed value.
A useful decision: remove(value) asks “Which value?” pop(index) asks “Which position?” The number in remove(1) is a value to find; the number in pop(1) is index 1.
items = ["book", "pen", "bag"]
items.remove("pen")
removed = items.pop(1)
print(items)
print(removed)remove("pen") removes by value. The list becomes ["book", "bag"], so index 1 now refers to "bag". Then pop(1) removes "bag" and stores that returned value in removed. Indexes belong to the list’s current state and can change after insert(), remove(), or pop().
items = ["book", "bag"]
items.insert(1, "pen")
print(items)insert(index, value) puts a new item at that position and shifts later items right: ["book", "bag"] → ["book", "pen", "bag"]. This differs from items[index] = value, which replaces the existing item instead of adding a new one.
items = [1, "one", 2]
items.remove(1)
removed = items.pop(1)
print(items)
print(removed)remove(1) finds the value 1; it does not mean “remove index 1.” After that removal, pop(1) removes the current item at index 1 and returns it. Use pop() with no argument when you want to remove and keep the final item.
tasks = ["Learn Python", "Practice loops"]
print(len(tasks))
print("Learn Python" in tasks)
if "Practice loops" in tasks:
tasks.remove("Practice loops")len(tasks) asks “How many items are in the list?” Its last valid index would be len(tasks) - 1. The expression value in list gives a Boolean result, connecting lists to bool and if. Checking first is useful because remove() can raise ValueError when the value is absent.
tasks = ["learn", "practice"]
tasks.append("build")
tasks.insert(1, "review")
tasks.remove("practice")
last_task = tasks.pop()
print(tasks)
print(last_task)
print(len(tasks))Pause before running it. Predict the final list, the value in last_task, and len(tasks). The list changes one operation at a time: ["learn", "practice"] → ["learn", "practice", "build"] → ["learn", "review", "practice", "build"] → ["learn", "review", "build"] → ["learn", "review"]. last_task is "build", and the final length is 2.
Choose an operation by your intention
- Add at the endUse
append(value)when the new item belongs after the current final item. - Add at a positionUse
insert(index, value)when the new item belongs before the item currently at that index. - Remove deliberatelyUse
remove(value)when you know the value; usepop(index)when you know the position; usepop()for the final item. - Check the current stateUse
len(list)to count items andvalue in listto check whether a value is present before removing it.
Common beginner mistakes
- Confusing a value with a position
remove(1)looks for the value1;pop(1)removes the item at index1. - Removing a value that is not present
items.remove("missing")can raiseValueError. Useif "missing" in items:when the value might be absent. - Forgetting that indexes changeAfter
insert(),remove(), orpop(), an old index may no longer point to the same item. - Forgetting that pop returns a value
removed = items.pop()does not only shorten the list: it gives you the removed final value so you can use it later.
