Indexing and Slicing Lists
The remove() method deletes the first occurrence of a value from a list when you know the value but not its index.
Try it: Indexing and Slicing
How Python indexes a sequence from 0 (or from the end with negative numbers) and how a slice start:stop:step picks positions — with the exact CPython rules for omitted and out-of-range bounds.
How it works
- Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
- An index outside the string raises IndexError.
- A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
- A negative step walks backwards; a step of 0 is a ValueError.
Default run (7 steps): 'PYTHONIC'[1:6:2]: a slice copies characters from start up to (not including) stop, moving by step. … 'PYTHONIC'[1:6:2] → 'YHN' (positions 1, 3, 5).
Simplified: Works on a short string (up to 16 printable ASCII characters); lists slice by exactly the same rules.
Loading the simulation…
Choosing the Removal Tool
List deletion depends on what you know. If you know the value you want to remove but not its position, use remove(). If you know the position, use del with an index. If you know a consecutive range of positions, use del with slice notation.
| Operation | What determines the deletion | How many elements it can remove |
|---|---|---|
| remove(value) | The first matching value | One element |
| del list[index] | One position | One element |
| del list[start:stop] | A range of positions | Multiple consecutive elements |
The value or position you provide determines which list elements are deleted.
Removing a Known Value
The remove() method is for the situation in which you know the value to delete but do not know where that value occurs in the list. It searches for the value and deletes its first occurrence. For example, applying remove('b') to a list containing 'a', 'b', and 'c' deletes 'b', leaving 'a' and 'c'. The list becomes shorter, and the remaining elements shift to fill the gap.
Reading a Deletion Slice
Deleting the Middle Range
A list contains the six values 'a', 'b', 'c', 'd', 'e', and 'f'. Which values are deleted by del t[1:5], and what remains?
- 1Read the start
The start index is 1, so the selected range begins with the element at index 1, which is 'b'.
- 2Read the stop
The stop index is 5, but the right boundary is exclusive. Index 5 itself is not selected.
- 3List the selected indices
The selected indices are 1, 2, 3, and 4. Their values are 'b', 'c', 'd', and 'e'.
- 4Inspect what remains
The values at indices 0 and 5 remain: 'a' and 'f'. The original element at index 5 becomes index 1 in the shorter list.
del t[1:5] removes 'b', 'c', 'd', and 'e', leaving 'a' and 'f'.
For del t[start:end], the start index is included and the end index is excluded. Therefore, del t[1:5] removes indices 1, 2, 3, and 4, not index 5.
Tracking the Shift
Deletion changes the positions of elements that remain after the removed portion. When one element is removed, later elements shift to fill the gap. When a range is removed, the elements after that range shift left by the number of deleted elements. In the six-element example, 'f' originally has index 5. After indices 1 through 4 are deleted, 'f' is at index 1.
Common Deletion Mistakes
Common mistake Treating remove() as if it removes an index
What it looks likeUsing remove(2) when the goal is to delete the element at index 2
Why it failsremove()searches for the value 2. It does not interpret 2 as a position.Do this insteadUse del list[2] when the position to delete is index 2.
Common mistake Assuming the stop index in a deletion slice is included
What it looks likeInterpreting del t[1:5] as deleting indices 1 through 5
Why it failsThe right slice boundary is exclusive.
Do this insteadRead del t[1:5] as deleting indices 1, 2, 3, and 4.
Common mistake Expecting remove() to return the deleted value
What it looks likeSaving the result of remove() and expecting it to be the removed element
Why it failsremove()returns None and modifies the list in place.Do this insteadInspect the list after the call if you need to see the result of the deletion.
Common mistake Continuing to use old positions after deletion
What it looks likeExpecting the element originally at index 5 to remain at index 5 after indices 1 through 4 are removed
Why it failsRemaining elements shift to fill the removed range.
Do this insteadRe-evaluate the indices after deletion; in the source example, the original index-5 element becomes index 1.
Before using del with a slice, write out the start index, the stop index, and the indices between them. Then check that the stop index is not part of the deletion. This makes the exclusive right boundary explicit.
Check Your Understanding
A list contains the values 'a', 'b', 'c', 'd', 'e', and 'f'. Decide which operation fits each goal: remove the first 'd' when its position is unknown; delete the element at index 2; delete the consecutive elements at indices 1, 2, and 3.
Hints
- Use
remove()when the value is known. - Use del with one index when one position is known.
- Use del with a slice whose start is 1 and whose exclusive stop is after index 3.
What do you think happens?
For the list 'a', 'b', 'c', 'd', 'e', 'f', what remains after del t[1:5]?
Reveal answer
['a', 'f']
The slice selects indices 1, 2, 3, and 4. Index 5 is the exclusive stop boundary, so 'f' remains.
Key Takeaways
- 1Use
remove(value)when you know the value but not its position; it deletes the first occurrence. - 2
remove()modifies the list in place and returns None. - 3Use del with one index to remove an element by position.
- 4Use del list[start:stop] to remove multiple consecutive elements.
- 5The stop boundary of a slice is exclusive, and elements remaining after deletion shift to new positions.
At a glance
remove()deletes the first matching value, not an element at a specified index.- del with an index removes one element by position.
- del with a slice removes a consecutive range in one operation.
- Slice stops are exclusive, so del t[1:5] removes indices 1 through 4.
- After deletion, remaining elements shift to fill the gap.