List Slicing and Copying
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.
One Statement, Two Results
A list method call can have two effects that must be tracked separately: it can change the list itself, and it can produce a return value. For append, extend, and sort, Python changes the existing list in place and the method call returns None. This combination is the source of a common mistake: the list changes successfully, but assigning the method call back to the list variable replaces the variable's list value with None.
In-Place Change and Return Value
Think of append, extend, and sort as instructions applied to an existing list. The instruction changes that list rather than producing a replacement list for you to store. At the same time, the method call has a result: None. Therefore, after a call such as items.append(value), inspect items to see the changed list, but do not expect the call itself to be a new list.
items = [3, 1]
result = items.append(5)
print(items)
print(result)- 1Before the call, items contains [3, 1].
- 2The append operation changes items to [3, 1, 5].
- 3The method call returns None, so result refers to None.
- 4The list and the return value are different things: items is the changed list, while result is None.
- items starts as a list with two elements.
- append changes the existing list by adding 5.
- result receives the method call's return value, which is None.
[3, 1, 5]
NoneThree List Operations
| Method | Operation on the list | Return value |
|---|---|---|
| append | Adds an element to the list | None |
| extend | Adds elements from another iterable to the list | None |
| sort | Changes the order of the list's contents | None |
The source identifies append, extend, and sort as in-place list methods that return None.
append, extend, and sort all modify a list in place, but they do different jobs. append adds an element. extend adds elements from another iterable. sort changes the order of the list's contents. The difference between append and extend is especially important when combining lists or working with nested data because the argument is treated differently by the two methods.
[1, 2, [3, 4]]
[1, 2, 3, 4]Tracing a Sort Call
What do you think happens?
After this code runs, what are the values of scores and returned?
Reveal answer
scores is [1, 3, 5] and returned is None
sort changes the existing list in place and returns None. The list's contents are reordered, but the method call does not produce a replacement list.
scores = [5, 3, 1]
returned = scores.sort()
print(scores)
print(returned)- 1Before sort, scores contains [5, 3, 1].
- 2After
scores.sort(), the same list contains [1, 3, 5]. - 3The call
scores.sort()returns None. - 4The variable returned therefore contains None rather than a sorted list.
- scores starts with the contents [5, 3, 1].
- sort changes the order of the existing list.
- returned receives None from the sort method call.
[1, 3, 5]
NoneThe Assignment Trap
items = [1, 2]
items = items.append(3)
print(items)The method changes the list to [1, 2, 3], then the assignment stores None in items.
items = [1, 2]
items.append(3)
print(items)The method changes items in place, and items remains the changed list [1, 2, 3].
Common mistake Assigning append back to the list variable.
What it looks likeitems = items.append(3)Why it failsappend changes items in place but returns None. The assignment therefore replaces the list stored in items with None.
Do this insteadCall items.append(3) without assigning its result.
Common mistake Assigning extend back to the list variable.
What it looks likeitems = items.extend([3, 4])Why it failsextend changes the existing list and returns None.
Do this insteadUse items.extend([3, 4]) by itself, then use items to inspect the changed list.
Common mistake Assigning sort back to the list variable.
What it looks likescores = scores.sort()Why it failssort reorders the existing list and returns None, so the assignment discards the list value held by scores.
Do this insteadUse scores.sort() without assignment.Common mistake Treating append and extend as identical operations.
What it looks likeUsing append when the elements of another iterable should be added separately.
Why it failsappend adds an element, while extend adds elements from another iterable. Their arguments are treated differently.
Do this insteadChoose append when adding one element and extend when adding elements from another iterable.
Practice the Trace
For each statement, predict the final list and the method call's return value before checking your reasoning: values = [4, 2]; returned = values.append(6). Then repeat with values = [4, 2]; returned = values.extend([6, 8]). Finally, use values = [4, 2]; returned = values.sort().
Hints
- Separate the list's state from the value stored in returned.
- append adds one element.
- extend adds elements from another iterable.
- sort changes the order of the existing list.
Tracing extend
Determine the final values after values = [4, 2]; returned = values.extend([6, 8]).
- 1Initial state
values contains [4, 2].
- 2Apply the method
extend adds the elements from [6, 8] to the existing list, so values becomes [4, 2, 6, 8].
- 3Track the return value
extend modifies the list in place and returns None, so returned becomes None.
values is [4, 2, 6, 8], and returned is None.
Practical Boundary
The source material for this lesson establishes the behavior of append, extend, and sort: each operates in place and returns None. It does not specify slice syntax or separate copying behavior, so those details should not be inferred from these three methods. When analyzing a list statement, identify the exact method being called first, then trace its in-place change and its return value.
- 1The central distinction is between changing a list and returning a value. append adds one element, extend adds elements from another iterable, and sort changes the list's order. All three modify the existing list in place and return None. Use these methods without assigning their results back to the list variable. When debugging, trace the list state and the method return value as two separate results.
Key Takeaways
- append, extend, and sort modify a list in place.
- append adds an element, extend adds elements from another iterable, and sort changes the list's order.
- Each of these method calls returns None.
- Assigning the result of one of these methods back to the list variable replaces the variable's list value with None.
- To debug a list operation, trace the changed list and the return value separately.