Removing Elements by Value with remove
pop removes an element by index and returns it; del removes an element by index without returning it.
The Choice Behind Removal
When you need to remove an element from a particular position in a Python list, two tools are especially important: pop and del. Both change the original list in place, but they communicate different intentions. Use pop when the removed value still matters to your program. Use del when you simply want to discard the element.
The central difference is simple: pop removes by index and returns the removed value; del removes by index without returning it.
Following an Element Through pop
What do you think happens?
A list contains ['a', 'b', 'c']. If pop is used at index 1, what happens to the list and what value can the program keep?
Reveal answer
Answer: The list becomes ['a', 'c'] and the removed value is 'b'.
Index 1 identifies 'b'. pop removes that element, shortens the list, and returns 'b', so the program can assign the returned value to a variable.
The index supplied to pop identifies the position to remove. In the source example, pop(1) removes 'b' from the list ['a', 'b', 'c']. The shortened list is ['a', 'c'], and the returned value is 'b'. Assigning that return value to a variable allows the program to use the removed element later.
Tracking the Index Shift
Removing an element changes the index positions of elements that were to its right. Every later element shifts one position to the left, so its index decreases by one. In ['a', 'b', 'c'], 'a' begins at index 0, 'b' at index 1, and 'c' at index 2. After index 1 is removed, 'a' remains at index 0 and 'c' moves to index 1.
Discarding with del
del also removes a list element by index and produces the same shortened list. If del removes index 1 from ['a', 'b', 'c'], the result is ['a', 'c']. The difference is that del does not return 'b' for later use; it simply discards the removed element.
Because del is a statement rather than an expression that returns a value, it cannot be used as the right-hand side of an assignment. Trying to capture its result, as in assigning del t[1] to a variable, produces a syntax error. This is why del is appropriate when the removed value is not needed.
| Tool | Removal target | What happens to the removed value | Best choice when |
|---|---|---|---|
| pop | An element at a specified index | The value is returned | You need to capture or use the value |
| del | An element at a specified index | The value is discarded | You only want to remove the element |
Common Selection Mistakes
Choosing del when the removed value is needed later.
del discards the removed value and provides no return value to capture.
Fix:
Choose pop when the program needs to capture or use the removed value.Assuming later elements keep their original indices.
Elements to the right of the removed position shift one place to the left.
Fix:
Reconsider the indices after every removal; the original index 2 becomes index 1 in the three-element example.Trying to assign the result of del.
del is a statement, not an expression that returns a value, so this produces a syntax error.
Fix:
Use pop when a returned value must be assigned to a variable.Using pop without considering intent.
Although using pop in this situation is not wrong, del communicates the discard intention more clearly and is slightly more efficient.
Fix:
Use del when the removed value is simply being discarded.
Practice the Decision
A list contains three elements. You need to remove the element at index 1, and another part of the program must use the removed value afterward. Which tool should you choose, and what should you expect to happen to the elements that were after index 1?
Hints
- Choose the tool that returns the removed element.
- The elements to the right of the removed position shift one index to the left.
A second list needs to lose its element at index 1, but the removed value has no further use. Which tool better communicates that intention?
Hints
- This case does not require a return value.
- The list still changes in place, and later elements still shift left.
For both practice situations, removing the middle element changes the list and shifts every later element down by one index. The deciding question is whether the removed value must be retained.
Key Takeaways
- pop removes an element by index and returns the removed value.
- del removes an element by index and discards the removed value.
- Both tools modify the list in place.
- Every element to the right of a removed position shifts one index to the left.
- Choose pop when the removed value is needed; choose del when it is not.
Key Takeaways
- Use pop to remove a list element by index and retain its value.
- Use del to remove a list element by index when the value should be discarded.
- Removing an element shifts all later elements one position to the left.
- The choice between pop and del depends mainly on whether the removed value is needed afterward.