Other List Methods: insert, remove, pop
append adds a single element to the end of a list, treating the argument as one indivisible object
Watching List Structure Change
List methods can change both the values in a list and the positions used to reach those values. The most important distinction in this group is that append adds one argument as one element, while extend adds the elements from an iterable individually. Insertion and removal also affect positions: inserting an element shifts existing elements, while removing an element causes later elements to move into earlier positions.
Adding at the End
append adds a single element to the end of a list and treats its argument as one indivisible object. The argument may itself be a list, but append still gives that entire list one position in the original list. extend behaves differently: it unpacks an iterable and adds each of its elements individually to the end.
Counting Positions After Several Additions
A list begins as ['apple', 'banana']. First add 'orange' with append, then add ['grape', 'kiwi'] with extend, and finally add ['mango', 'peach'] with append. What is the final list?
First addition: Appending 'orange' adds one element at the end, producing ['apple', 'banana', 'orange'].
Second addition: Extending with ['grape', 'kiwi'] adds both strings individually, producing ['apple', 'banana', 'orange', 'grape', 'kiwi'].
Third addition: Appending ['mango', 'peach'] adds the supplied list as one nested element.
['apple', 'banana', 'orange', 'grape', 'kiwi', ['mango', 'peach']]
Placing an Element
insert places one element at a specified index. Because the new element occupies that position, existing elements at that index and later positions shift to make room. The list therefore contains the inserted value as well as its original values, but the indexes of affected elements change.
Removing by Value
remove finds and deletes the first matching value. If a list contains duplicate values, the first occurrence is the one removed. Elements that came after the removed value then occupy the positions that follow the deletion.
Removing by Index
pop removes an element by index and returns the removed value. Removing an indexed element changes the positions of elements that followed it, because those elements move into the gap left by the removed value. When tracing pop, track both results: the new list contents and the value returned by the operation.
Tracing Several Operations
When several list methods are used in sequence, record the list after every operation. First identify whether an operation adds one object, adds several iterable elements, inserts at a position, removes the first matching value, or removes by index. Then update the indexes before analyzing the next operation.
For every operation, write down the list immediately afterward. For append and extend, count how many new positions are added. For insert, mark the insertion index and shift later positions. For remove, locate the first matching value. For pop, record the removed value separately because it is returned while the list is changed.
Mistakes with List Methods
Expecting append to add the contents of a list separately
append treats its argument as one indivisible object.
Fix:
Use extend when the elements of an iterable should be added individually.Expecting append or extend to return the modified list
The operation changes the original list rather than producing a list result for the caller.
Fix:
Inspect the original list after the method call.Forgetting that insertion changes later indexes
The original index map no longer describes the changed list.
Fix:
Recalculate the positions of elements at and after the insertion point.Treating remove and pop as the same operation
The two operations use different ways to identify what leaves the list.
Fix:
Choose remove when the target is a value and pop when the target is an index.
Practice and Review
A list contains ['north', 'south']. Predict the list structure after appending ['east', 'west']. Then predict the structure if the original list is reset and extended with ['east', 'west']. Finally, describe what changes when an item is inserted between 'north' and 'south', when the first occurrence of a duplicate value is removed, and when an indexed item is popped.
Hints
- append adds its argument as one element.
- extend adds the iterable's elements individually.
- insert makes room at an index.
- remove targets the first matching value.
- pop targets an index and returns the removed value.
- append adds one argument as a single element at the end of a list. extend unpacks an iterable and adds its elements individually. insert places one element at an index and shifts later elements. remove deletes the first matching value. pop removes an element by index, returns that value, and changes the positions of elements that followed it. The safest way to predict a sequence of operations is to record the list after every step.
Key Takeaways
- append treats its argument as one element, even when that argument is a list.
- extend adds each element from an iterable individually and merges those elements into the original list.
- insert adds an element at a specified index and shifts later elements.
- remove deletes the first matching value, while pop removes by index and returns the removed value.
- Tracing the list after every method call makes changed indexes and nested structures easier to predict.