Iterating Over Lists
append adds a single element to the end of a list, treating the argument as one indivisible object
One Argument, Two Structures
Python provides two list methods for adding elements: append and extend. Both add something to the end of a list, but they do not incorporate their arguments in the same way. The important question is whether the argument should become one complete element or whether its individual elements should be added separately.
Tracing the List State
Adding several fruits
Start with ['apple', 'banana']. Add 'orange' as one element, add 'grape' and 'kiwi' individually from another list, and then add ['mango', 'peach'] as one element.
Starting state: The list is ['apple', 'banana'] and contains two elements.
Add orange: Appending 'orange' adds one new element, producing ['apple', 'banana', 'orange'].
Add grape and kiwi: Extending with ['grape', 'kiwi'] adds both elements separately, producing ['apple', 'banana', 'orange', 'grape', 'kiwi'].
Add mango and peach as one object: Appending ['mango', 'peach'] adds the list argument as one nested element.
['apple', 'banana', 'orange', 'grape', 'kiwi', ['mango', 'peach']]
The list structure is the key evidence. After append, the argument occupies one position, even when that argument is itself a list. After extend, the contents of the argument occupy separate positions. In the fruit example, the nested list counts as one element, so the final list contains six elements rather than seven.
The Two Method Rules
append adds a single element to the end of a list and treats its argument as one indivisible object. The argument can be a number, a string, another list, or another object; whatever it is, it becomes one new position in the list.
extend unpacks an iterable and adds each of its elements individually to the end of the list. Use it when the goal is to add all elements from another collection into the existing list.
Where the Results Diverge
A useful trace follows the list after each operation. Begin with ['apple', 'banana']. Adding 'orange' with append creates a three-element list. Extending with ['grape', 'kiwi'] adds two separate elements, bringing the total to five. Appending ['mango', 'peach'] adds one nested element, bringing the total to six.
When tracing an operation, count positions rather than counting every value written inside a nested element. The nested list ['mango', 'peach'] contains two values, but it occupies only one position in the outer list because it was added with append.
Mistakes in List Building
Using append when the goal is to merge the contents of another list
append always treats its argument as one indivisible object.
Fix:
Use extend when each element from the argument should become a separate element in the original list.Expecting extend to preserve the argument as one nested list
extend unpacks an iterable and adds its elements individually.
Fix:
Use append when the complete list must remain one element.Counting values inside a nested list as separate positions in the outer list
The nested list is one object stored at one position in the outer list.
Fix:
Trace the outer list's positions and treat the nested list as one element.Expecting either method to create a new list result
The methods perform the update on the original list rather than returning the modified list.
Fix:
Inspect the original list after the operation and remember that the method return value is None.
Choosing the Right Operation
| Goal | Method | Effect on the outer list |
|---|---|---|
| Add one complete object | append | The argument becomes one new element |
| Add every element from a collection | extend | The iterable's elements become separate new elements |
| Keep another list grouped as one element | append | The other list becomes a nested element |
| Merge another list's contents into the list | extend | The other list's contents are added individually |
A simple decision rule is: use append for one element and extend for the elements of a collection. Ask what should occupy the next position in the list. If the answer is one complete object, choose append. If the answer is several individual elements from an iterable, choose extend.
Predict the final list structure in each case. First, a list receives another list with append. Second, the same starting list receives that list with extend. For each case, state how many elements the outer list contains and whether the added list is nested.
Hints
- append treats its argument as one element.
- extend adds the argument's elements individually.
- Count positions in the outer list, not values inside a nested element.
Key Takeaways
- append adds its argument as one element at the end of a list.
- extend unpacks an iterable and adds its elements individually.
- Appending a list creates a nested element; extending with a list merges its contents into the outer list.
- Both methods modify the original list in place, return None, and do not modify the argument.
- Choose append for one complete object and extend for adding the elements of a collection.
Key Takeaways
- append treats its argument as one indivisible list element.
- extend unpacks an iterable and adds each element separately.
- The same list argument produces a nested structure with append and a merged structure with extend.
- Both methods change the original list in place and return None.
- Select the method according to whether you need one new object or several new elements.