Modifying Lists: Assignment and Methods
Both modify the original list in place.
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…
One Removal, Two Intentions
When you need to remove an element at a particular position in a Python list, two tools are available: the pop method and the del statement. Both modify the original list in place. Their important difference is what happens to the removed element: pop returns it so you can capture and use it, while del simply discards it.
Choose pop when the removed value matters afterward. Choose del when you only want the element gone.
Tracing a Removal
What do you think happens?
Suppose t is ['a', 'b', 'c']. After removing the element at index 1, what index will 'c' have?
Reveal answer
Index 1
The element at index 1 is removed. Every element to its right shifts one position to the left, so 'c' moves from index 2 to index 1.
Removing an element changes the positions of elements after it. The element being removed disappears, and every element that was to its right shifts down by one index position. Elements before the removed position keep their indices. This shifting happens whether the removal is performed with pop or with del.
Capturing with pop
pop removes an element by index and returns the removed element. Assigning the result of pop to a variable lets you use that removed value after the list has been shortened.
t = ['a', 'b', 'c']
x = t.pop(1)- 1The list t has 'a' at index 0, 'b' at index 1, and 'c' at index 2.
- 2The call
t.pop(1)removes 'b' from index 1. - 3Because pop returns the removed element, x receives 'b'.
- 4The remaining 'c' shifts from index 2 to index 1.
- t starts with three elements.
pop(1)selects the element at index 1, which is 'b'.- The removed value is assigned to x.
- The original list t is modified in place and becomes ['a', 'c'].
x is 'b'; t is ['a', 'c']x is 'b'
t is ['a', 'c']Discarding with del
del removes an element from a list by index without returning the removed element. Use it when the element should simply be discarded.
t is ['a', 'c']Selecting the Right Tool
| Tool | How it removes | What happens to the removed value | Use it when |
|---|---|---|---|
| pop | Removes an element by index | Returns the removed element | You need to capture or use the value |
| del | Removes an element by index | Does not return the removed element | You simply want to discard the value |
Both pop and del modify the list in place; their key difference is whether the removed value is returned.
Let your choice communicate your intention. Use pop when later code needs the removed element. Use del when removal is the only goal. Using pop without needing its return value is not wrong, but del is clearer about the fact that the value is being discarded and is slightly more efficient according to the source material.
Mistakes with Removal
Common mistake Expecting del to provide a value
What it looks likex = del t[1]Why it failsdel is a statement and does not return the removed element.
Do this insteadUse x = t.pop(1) when the removed value must be captured.
Common mistake Forgetting that later indices change
What it looks likeIn ['a', 'b', 'c'], assuming 'c' remains at index 2 after removing index 1.
Why it failsEvery element to the right of the removed element shifts one position to the left.
Do this insteadAfter removing 'b', refer to 'c' at index 1.
Common mistake Choosing the method without considering intent
What it looks likeUsing pop when the removed value is simply being discarded.
Why it failsThe code does not clearly communicate that no returned value is needed.
Do this insteadUse del when you only want to remove the element; use pop when you need the value afterward.
Apply the Choice
A list named tasks contains ['read', 'code', 'test']. Write one statement that removes the element at index 1 and stores the removed value in a variable named current_task. Then write a separate version that removes the element at index 1 without storing the removed value.
Hints
- The first version needs the removed value, so choose the tool that returns it.
- The second version only needs to discard the element.
- The index is written inside pop parentheses or square brackets, depending on the tool.
Choosing by Intent
Remove 'code' from tasks at index 1, first keeping the removed value and then discarding it.
- 1Keep the value
Use
current_task=tasks.pop(1), because pop returns the element removed at index 1. - 2Discard the value
Use del tasks[1], because del removes the element at index 1 without returning it.
- 3Track the remaining list
After either operation, the list contains ['read', 'test']; 'test' has shifted from index 2 to index 1.
Use pop when the removed value is needed later. Use del when it is not needed.
Key Takeaways
- 1pop removes an element by index and returns the removed value.
- 2del removes an element by index without returning it.
- 3Both tools modify the original list in place.
- 4After removal, every element to the right shifts one index position to the left.
- 5Choose pop when you need the removed value and del when you simply want to discard it.
At a glance
- Use
pop(index)to remove an element and capture the value it returns. - Use del list[index] to remove an element when its value is not needed.
- Removing an element shifts every later element one position to the left.
- Both pop and del change the list in place.
- The need for the removed value determines which tool communicates your intent.