Functions and Return Values
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.
A Changed List Is Not a Returned List
When you call a list method, two results must be considered separately: what happens to the list itself and what value the method call produces. The methods append, extend, and sort modify the existing list in place. Each of them returns None. That means the list can change even though the method call does not produce a new list value.
Tracing an In-Place Method Call
Calling append
Suppose a list starts as [10, 20]. What are the list and the return value after append(30) is called?
Starting state: The list contains 10 and 20.
Modify the list: append adds 30 as one new item in the existing list.
Identify the return value: append modifies the list in place and returns None.
The list is [10, 20, 30], and the return value of the method call is None.
items = [10, 20] result = items.append(30) print(items) print(result)
Choosing Between Append and Extend
append and extend both add elements to a list, but they treat their arguments differently. append adds its argument as one item. extend takes the items from another iterable and adds those items to the list. This difference matters when the argument itself contains several values: append can place that argument into the list as a single item, while extend adds the contained items separately.
| Call | Resulting list | What was added |
|---|---|---|
| first.append([3, 4]) | [1, 2, [3, 4]] | One item: the list [3, 4] |
| second.extend([3, 4]) | [1, 2, 3, 4] | The items 3 and 4 separately |
append and extend make different choices about the argument's structure.
Sorting the Existing List
sort is another in-place list method. Its operation is different from append and extend: instead of adding elements, sort rearranges the existing list. Immediately after the call, inspect the same list variable to see its reordered contents. The return value is still None.
[10, 30, 40]
NoneThe Assignment Trap
The most common mistake is assigning the result of an in-place list method back to the list variable. Because append, extend, and sort return None, the assignment replaces the variable's reference with None. The list operation has already happened, but the variable no longer refers to the list afterward.
Assigning append's result back to the list variable
append modifies the list in place and returns None, so items becomes None.
Fix:
Call items.append(3) without assigning its result.Assigning extend's result back to the list variable
extend modifies the list in place and returns None, so items becomes None.
Fix:
Call items.extend([3, 4]) without assigning its result.Assigning sort's result back to the list variable
sort rearranges the list in place and returns None, so scores becomes None.
Fix:
Call scores.sort() and then use scores.
A Reliable Tracing Routine
- Write down the list's state before the method call.
- Identify which operation the method performs: append adds one item, extend adds items from another iterable, or sort rearranges the existing contents.
- Update the list's state in place.
- Record the method's return value as None.
- Check whether an assignment stores that return value back into the list variable.
- If such an assignment is present, update the variable to None.
What do you think happens?
After these statements, what are the values of items and result? items = [2, 1] result = items.sort()
Reveal answer
Answer: items is [1, 2] and result is None
sort rearranges the existing list in place, while its return value is None.
Practice the State Trace
Trace each snippet without running it. For every snippet, write the final list state and the method call's return value. Then identify whether the list variable still refers to the list. A. values = [1, 2] result = values.extend([3, 4]) B. values = [3, 1, 2] result = values.sort() C. values = [1, 2] values = values.append(3)
Hints
- For extend, decide whether the argument becomes one item or contributes its items separately.
- For sort, rearrange the existing contents and then record None as the return value.
- For the final snippet, look for an assignment that overwrites the list variable.
Practice Answer for Snippet A
Trace values = [1, 2] followed by result = values.extend([3, 4]).
Starting state: values contains [1, 2].
Apply extend: The items 3 and 4 are added separately, so the list becomes [1, 2, 3, 4].
Record the result: The method returns None, so result is None. Because the result was not assigned to values, values still refers to the changed list.
values is [1, 2, 3, 4], and result is None.
Key Takeaways
- append, extend, and sort modify a list in place.
- append adds its argument as one item, while extend adds the items from another iterable.
- sort rearranges the existing list.
- All three methods return None.
- Do not assign the result of these methods back to the list variable, or the variable will be overwritten with None.
Key Takeaways
- Separate a method's side effect from its return value.
- Use append for adding one item, extend for adding items from another iterable, and sort for rearranging a list.
- Expect append, extend, and sort to return None.
- Never replace a list variable with the result of one of these in-place methods.