Other List Methods: insert, remove, pop
If a list contains duplicate values, the first occurrence is the one removed.
Try it: List Methods, Step by Step
What append, extend, insert, remove, pop, del, index, count, sort and reverse really do to a Python list — which items shift, what each returns, CPython's exact error messages — and why lst = lst + other makes a new list while += changes it in place.
How it works
- append/extend add at the end; nothing else moves.
- insert(i, x) resolves i (negative counts from the end; out of range is clamped, never an error), shifts the tail right, then places x.
- remove(x) scans for the FIRST equal item (ValueError if none); pop(i) and del lst[i] take one position (IndexError if out of range); the tail shifts left.
- index/count scan and return a number; sort/reverse rearrange in place and return None.
- lst + other builds a new list object and rebinds the name; lst += other extends the same object.
Default run (5 steps): lst = [30, 10, 20, 10] — list object #1, 4 items. Choose a method. Example: lst.insert(1, 99). … lst.insert(1, 99) returned None. lst is now [30, 99, 10, 20, 10].
Simplified: A list of up to 12 ints; sort() is shown as its result, not step by step.
Loading the simulation…
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?
- 1First addition
Appending 'orange' adds one element at the end, producing ['apple', 'banana', 'orange'].
- 2Second addition
Extending with ['grape', 'kiwi'] adds both strings individually, producing ['apple', 'banana', 'orange', 'grape', 'kiwi'].
- 3Third 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
Common mistake Expecting append to add the contents of a list separately
What it looks likeAppending ['d', 'e'] produces a nested element rather than two new top-level elements.
Why it failsappend treats its argument as one indivisible object.
Do this insteadUse extend when the elements of an iterable should be added individually.
Common mistake Expecting append or extend to return the modified list
What it looks likeThe source material states that both methods modify the original list in place and return None.
Why it failsThe operation changes the original list rather than producing a list result for the caller.
Do this insteadInspect the original list after the method call.
Common mistake Forgetting that insertion changes later indexes
What it looks likeAn element inserted into the middle occupies the requested index and shifts existing elements after it.
Why it failsThe original index map no longer describes the changed list.
Do this insteadRecalculate the positions of elements at and after the insertion point.
Common mistake Treating remove and pop as the same operation
What it looks likeremove selects a matching value, whereas pop removes by index and returns the removed value.
Why it failsThe two operations use different ways to identify what leaves the list.
Do this insteadChoose 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.
- 1append 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.