Understanding Mutability
Python provides several methods for common list operations. Three of the most frequently used are append, extend, and sort. Each modifies the list in place and returns None, but they perform different operations on the list's contents.
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
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 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)
[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
Assigning the result of an in-place method back to the list variable
sort changes the list in place and returns None, so the assignment replaces the value of items with None.
Fix:
Call items.sort() without assigning its result.Treating append and extend as interchangeable
append and extend both add elements, but they treat the argument differently: append adds one item, while extend adds multiple elements.
Fix:
Choose append for one item and extend for multiple elements.Checking only the return value and ignoring the changed list
These methods modify the list in place even though they return None.
Fix:
Trace 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]).
Start: The list is [5, 2].
Apply the method: extend adds multiple elements to the existing list, so the list becomes [5, 2, 8, 9].
Record 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
- append, extend, and sort modify a list in place.
- append adds one item, extend adds multiple elements, and sort reorders the existing list contents.
- Each of these methods returns None.
- Do not assign the result of one of these methods back to the list variable, because that assignment overwrites the variable with None.
- When 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.