Modifying Lists: Assignment and Methods
pop removes an element by index and returns it; del removes an element by index without returning it.
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
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)
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 |
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
Expecting del to provide a value
del is a statement and does not return the removed element.
Fix:
Use x = t.pop(1) when the removed value must be captured.Forgetting that later indices change
Every element to the right of the removed element shifts one position to the left.
Fix:
After removing 'b', refer to 'c' at index 1.Choosing the method without considering intent
The code does not clearly communicate that no returned value is needed.
Fix:
Use 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.
Keep the value: Use current_task = tasks.pop(1), because pop returns the element removed at index 1.
Discard the value: Use del tasks[1], because del removes the element at index 1 without returning it.
Track 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
- pop removes an element by index and returns the removed value.
- del removes an element by index without returning it.
- Both tools modify the original list in place.
- After removal, every element to the right shifts one index position to the left.
- Choose pop when you need the removed value and del when you simply want to discard it.
Key Takeaways
- 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.