Removing Elements by Value with remove
The central difference is simple: pop removes by index and returns the removed value; del removes by index without returning it.
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…
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
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 |
Both tools modify the list in place, but pop returns the removed element and del does not.
Common Selection Mistakes
Common mistake Choosing del when the removed value is needed later.
What it looks likeA program removes an element and then expects a variable to contain that removed value.
Why it failsdel discards the removed value and provides no return value to capture.
Do this insteadChoose pop when the program needs to capture or use the removed value.
Common mistake Assuming later elements keep their original indices.
What it looks likeAfter removing the element at index 1, a program still treats the original element at index 2 as being at index 2.
Why it failsElements to the right of the removed position shift one place to the left.
Do this insteadReconsider the indices after every removal; the original index 2 becomes index 1 in the three-element example.
Common mistake Trying to assign the result of del.
What it looks likeA learner attempts to capture the result of deleting an indexed element.
Why it failsdel is a statement, not an expression that returns a value, so this produces a syntax error.
Do this insteadUse pop when a returned value must be assigned to a variable.
Common mistake Using pop without considering intent.
What it looks likeA program uses pop even though it has no use for the removed value.
Why it failsAlthough using pop in this situation is not wrong, del communicates the discard intention more clearly and is slightly more efficient.
Do this insteadUse 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
- 1pop removes an element by index and returns the removed value.
- 2del removes an element by index and discards the removed value.
- 3Both tools modify the list in place.
- 4Every element to the right of a removed position shifts one index to the left.
- 5Choose pop when the removed value is needed; choose del when it is not.
At a glance
- 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.