Mutating Lists: pop(), append(), and extend()
The remove() method deletes the first occurrence of a value from a list when you know the value but not its index.
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…
Choosing What to Remove
List removal depends on the information you have. If you know the value you want to remove but not its position, use remove(). If you know the position, del can remove one element by index. If you need to remove several consecutive positions, del can use slice notation.
The central distinction is value versus position: remove() searches for a value, while del uses an index or a range of indices.
Tracing remove()
Suppose a list contains the values 'a', 'b', and 'c'. Calling remove('b') searches the list for the value 'b'. Once that value is found, it is deleted. The remaining values are 'a' and 'c', so the list becomes shorter by one element.
The remove() method deletes the first occurrence of a value from a list when you know the value but not its index.
The Return Value
remove() changes the existing list in place. It does not return the element that was deleted; its return value is None. Therefore, assigning the result of remove() to a variable gives that variable None, while the original list has still been changed.
Separating the Changed List from the Return Value
A list contains 'a', 'b', and 'c'. You remove 'b' and capture the result of the operation. What does the list contain afterward, and what value does the captured result contain?
- 1Search
remove()searches for the value 'b' in the list. - 2Mutate
The first matching value is deleted, leaving 'a' and 'c' in the list.
- 3Check the result
The operation returns None rather than the removed value.
The list contains 'a' and 'c', and the captured return value is None.
Deleting a Consecutive Range
Use del with slice notation when several consecutive elements must be removed in one operation. For a list containing 'a', 'b', 'c', 'd', 'e', and 'f', the expression del t[1:5] removes the elements at indices 1, 2, 3, and 4. Those values are 'b', 'c', 'd', and 'e'.
After del t[1:5], 'a' remains at the beginning and 'f' remains at the end. The surviving 'f' shifts from its original index 5 to index 1 because the earlier elements were deleted.
Reading Slice Boundaries
A deletion slice follows the same boundary rule as the slice notation shown in the source: the start boundary is included and the end boundary is excluded. In del t[1:5], index 1 is included, while index 5 is not. The selected indices are therefore 1, 2, 3, and 4.
Value or Position
| Operation | What you provide | What it removes |
|---|---|---|
| remove(value) | A value | The first occurrence of that value |
| del t[index] | One index | The element at that position |
| del t[start:end] | A start and end boundary | All elements from start through the position before end |
Choose the operation according to whether your information is a value, one position, or a consecutive range of positions.
Common mistake Using remove() when you need to delete a specific position.
What it looks likeChoosing remove() when the intended target is the element at a known index.
Why it failsremove()searches by value, not by position.Do this insteadUse del with the known index.
Common mistake Expecting remove() to return the deleted value.
What it looks likeSaving the result of remove() and expecting it to contain the removed element.
Why it failsremove()returns None even though it changes the list.Do this insteadInspect the changed list separately; do not treat the return value as the removed element.
Common mistake Including the end boundary of a deletion slice.
What it looks likeInterpreting del t[1:5] as removing index 5 too.
Why it failsThe right boundary is exclusive.
Do this insteadCount indices 1, 2, 3, and 4; index 5 is not selected.
Common mistake Assuming a range deletion leaves later indices unchanged.
What it looks likeExpecting the element originally at index 5 to remain at index 5 after del t[1:5].
Why it failsThe deleted elements leave a gap, and the remaining elements shift to fill it.
Do this insteadRe-evaluate the positions of surviving elements after the deletion.
Check Your Reasoning
A list contains six values in order: 'a', 'b', 'c', 'd', 'e', 'f'. Determine which values are removed by del t[1:5], which value remains at the final position after the deletion, and whether remove() would be the right tool if you only knew that the value to delete was 'd'.
Hints
- Write out the indices from 0 through 5.
- For the slice 1:5, include 1 and stop before 5.
remove()is selected by value, while del with a slice is selected by position.
Applying Both Selection Rules
Use the source list 'a', 'b', 'c', 'd', 'e', 'f' to compare a value-based deletion of 'd' with the range deletion del t[1:5].
- 1Value-based choice
If the only information available is the value 'd',
remove()is the matching operation because it searches for a value. - 2Range-based choice
If the intended positions are 1 through 4, del t[1:5] selects those four consecutive indices.
- 3Compare the scope
The first operation targets the first matching value, while the second targets every element in the specified consecutive range.
remove() is appropriate when the target is identified by value. del with a slice is appropriate when the target is identified as a consecutive range of positions.
Key Takeaways
- 1
remove()deletes the first occurrence of a known value. - 2
remove()modifies the list in place and returns None. - 3del with slice notation removes multiple consecutive elements.
- 4For del t[start:end], start is included and end is excluded.
- 5Use
remove()for value-based selection and del with an index or slice for position-based selection.
At a glance
- Use
remove(value)when you know what value to delete but not where it appears. - Only the first occurrence is removed, and the list is changed in place.
- Use del with a slice to delete a consecutive range of positions.
- Slice deletion includes the start boundary and excludes the end boundary.
- Choose between
remove()and del by asking whether your target is identified by value or by position.