Understanding Mutability
Always trace the list and the return value separately. A method can change the list in place while its return value is None.
The List and the Return Value
When you call a list method, two questions matter: what happens to the list itself, and what value does the method call produce? Python's append, extend, and sort methods modify the existing list in place. Each one returns None. This means the list can change even though the method call does not produce a new list value.
Always trace the list and the return value separately. A method can change the list in place while its return value is None.
Tracing an In-Place Change
What do you think happens?
Suppose values starts as [3, 1]. After values.append(5), what are the list and the return value?
Reveal answer
The list is [3, 1, 5], and the return value is None.
append modifies the existing list in place and returns None. The changed list and the method's return value are separate results.
The diagram separates two outcomes that are easy to mix up. The list changes from [3, 1] to [3, 1, 5]. The call to append(5), however, evaluates to None. The changed list is the object you continue using; None is the method's return value.
Three Ways to Change Contents
| Method | Effect on the list | Return value |
|---|---|---|
| append | Adds one item to the list | None |
| extend | Adds multiple elements to the list | None |
| sort | Reorders the existing list's contents | None |
append, extend, and sort all modify the list in place, but their operations are different.
append and extend both add elements, but they treat their arguments differently. append adds one item to the list. extend adds multiple elements to the list. This distinction is especially important with nested data or when combining lists. sort does not add elements; it changes the order of the existing list contents. All three methods modify the original list and return None.
Method Calls in Practice
numbers = [3, 1]
append_result = numbers.append(5)
print(numbers)
print(append_result)
parts = [1, 2]
extend_result = parts.extend([3, 4])
print(parts)
print(extend_result)
scores = [4, 2, 3]
sort_result = scores.sort()
print(scores)
print(sort_result)- 1numbers starts as [3, 1].
append(5)changes it to [3, 1, 5], whileappend_resultreceives None. - 2parts starts as [1, 2].
extend([3, 4])changes it to [1, 2, 3, 4], whileextend_resultreceives None. - 3scores starts as [4, 2, 3].
sort()changes the order to [2, 3, 4], whilesort_resultreceives None.
[3, 1, 5]
None
[1, 2, 3, 4]
None
[2, 3, 4]
NoneThe assignment statements capture None, not a replacement list. The lists themselves have already been modified before the return values are assigned.
The Assignment Trap
Common mistake Assigning the result of an in-place method back to the list variable
What it looks likeitems = items.sort()Why it failssort changes the list in place and returns None, so the assignment replaces the value of items with None.
Do this insteadCall items.sort() without assigning its result.
Common mistake Treating append and extend as interchangeable
What it looks likeUsing append when the intention is to add multiple elements
Why it failsappend and extend both add elements, but they treat the argument differently: append adds one item, while extend adds multiple elements.
Do this insteadChoose append for one item and extend for multiple elements.
Common mistake Checking only the return value and ignoring the changed list
What it looks likeAssuming that a None return value means the list did not change
Why it failsThese methods modify the list in place even though they return None.
Do this insteadTrace the list after the call and separately record the method's return value.
Predict Before Running
For each call, predict both the final list and the method's return value.
1. values = [5, 2]
result = values.append(8)2. values = [5, 2]
result = values.extend([8, 9])3. values = [5, 2]
result = values.sort()Then explain why assigning values = values.sort() causes a problem.
Hints
- All three methods modify the existing list in place.
- All three methods return None.
- append adds one item, extend adds multiple elements, and sort reorders the existing contents.
Tracing extend
Determine the list and return value after values = [5, 2] followed by result = values.extend([8, 9]).
- 1Start
The list is [5, 2].
- 2Apply the method
extend adds multiple elements to the existing list, so the list becomes [5, 2, 8, 9].
- 3Record the return value
extend modifies the list in place and returns None, so result is None.
values is [5, 2, 8, 9], and result is None.
Working Rule
- 1append, extend, and sort modify a list in place.
- 2append adds one item, extend adds multiple elements, and sort reorders the existing list contents.
- 3Each of these methods returns None.
- 4Do not assign the result of one of these methods back to the list variable, because that assignment overwrites the variable with None.
- 5When debugging, trace the list's state and the method's return value as two separate outcomes.
Key Takeaways
- append, extend, and sort all change the existing list rather than producing a new list value.
- append adds one item, extend adds multiple elements, and sort changes the order of existing contents.
- The return value of each method is None.
- Calling a method without assignment preserves the changed list; assigning its result back replaces the variable with None.