Slicing Lists
Removing an element from a list is not one single operation.
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 Question
Removing an element from a list is not one single operation. Python provides pop, del, and remove because each answers a different question. Do you know where the element is, or only what the element is? Do you need the removed value afterward, or can it be discarded? Answering those questions first makes the correct technique easier to choose.
| Tool | What you provide | What it removes | What it returns |
|---|---|---|---|
| pop | An index | The element at that index | The removed value |
| del | An index or slice | One element or a range of elements | No value |
| remove | A value | The first occurrence of that value | None |
The three main list-removal tools solve different removal problems.
Indexing the List
Both pop and del use positions, or indices. In the list ['a', 'b', 'c'], index 1 identifies 'b'. Removing that element causes the list to shrink, so 'c', which was at index 2, shifts left to index 1. A negative index can also identify an element from the end of a list, while a slice identifies a range of positions.
items = ['a', 'b', 'c']
x = items.pop(1)- 1The list begins as ['a', 'b', 'c'].
- 2
pop(1)targets index 1, whose value is 'b'. - 3The list becomes ['a', 'c'].
- 4The variable x receives the returned value 'b'.
- items starts with three elements.
- Index 1 identifies 'b'.
- pop removes 'b' and returns it to x.
x is 'b'
items is ['a', 'c']Tracing Each Removal Tool
Use pop when you know the index and need the removed element. It removes that element, shortens the list, and returns the removed value. Use del when you know the index but do not need the removed value. del can also remove a range through a slice. Use remove when you know the value but not its index. It searches for that value and deletes its first occurrence.
t = ['a', 'b', 'c']
del t[1]- 1The list begins as ['a', 'b', 'c'].
- 2del t[1] removes 'b'.
- 3The list becomes ['a', 'c'].
- 4There is no returned value to store.
- t[1] identifies 'b'.
- del removes the element at that index.
- No removed value is produced for assignment.
t = ['a', 'b', 'c', 'd', 'e', 'f']
del t[1:5]- 1The selected values are 'b', 'c', 'd', and 'e'.
- 2The value at index 5, 'f', is outside the slice and remains.
- 3The list becomes ['a', 'f'].
- 4After deletion, 'f' shifts to index 1.
- The slice starts at index 1.
- The slice stops before index 5.
- Indices 1, 2, 3, and 4 are removed.
t = ['a', 'b', 'c']
t.remove('b')- 1The list is searched for the value 'b'.
- 2The value is found at index 1 and removed.
- 3The list becomes ['a', 'c'].
- 4The removed value is not returned.
- remove receives the value 'b', not an index.
- The first occurrence of 'b' is deleted.
- remove returns None.
Watching the List Change
The important state change is not only that an element disappears. Every element after the removed position can receive a new index. After removing 'b' from ['a', 'b', 'c'], 'c' moves from index 2 to index 1. After deleting the slice from index 1 through index 4, 'f' moves from index 5 to index 1.
Mistakes Beginners Make
Common mistake Trying to assign the result of del
What it looks likex = del t[1]Why it failsdel does not produce a value to assign.
Do this insteadUse del t[1] when the element should simply be removed. Use x = t.pop(1) when you need the removed value.
Common mistake Using remove when you need a returned value
What it looks likex = t.remove('b')Why it failsremove returns None rather than the removed element.
Do this insteadUse x = t.pop(index) when you know the index and need to capture the removed element.
Common mistake Confusing an index with a value
What it looks likeUsing remove when the task identifies a position, or using pop when the task identifies only a value.
Why it failspop and del work from indices, while remove searches for a value.
Do this insteadUse pop or del for a known index. Use remove for a known value whose position is not known.
Common mistake Calling remove for a value that is not present
What it looks likeCalling t.remove(value) when value does not occur in the list.
Why it failsThe requested value cannot be found and the removal operation cannot complete as intended.
Do this insteadConsider whether the value may be absent before choosing remove.
Common mistake Modifying a list while iterating over it
What it looks likeRemoving elements from a list during iteration.
Why it failsChanging the list during iteration can lead to common bugs.
Do this insteadTreat list modification during iteration as an edge case requiring careful handling.
Practice the Decision
For each situation, choose pop, del, or remove. Then predict the final list and state whether a removed value is available: a) remove the element at index 1 and save it; b) remove the element at index 1 without saving it; c) remove the first occurrence of the value 'b'; d) remove all elements at indices 1 through 4 from ['a', 'b', 'c', 'd', 'e', 'f'].
Hints
- Use pop when an index is known and the removed value must be captured.
- Use del for an indexed removal when no returned value is needed.
- Use remove when the value is known but its index is not.
- A slice includes its start index and excludes its end index.
Selecting the Tool from the Task
A list contains ['a', 'b', 'c']. You know that the target is at index 1, and you need to use the removed value later.
- 1Identify the information available
The target is identified by its index, so a value-based search with remove is not the best match.
- 2Identify the required result
The removed element must be available later, so the operation must return that element.
- 3Choose the operation
pop matches both requirements: it accepts an index and returns the removed value.
- 4Predict the state
Index 1 contains 'b'. Removing it leaves ['a', 'c'], and the captured value is 'b'.
Use pop(1). The list becomes ['a', 'c'], and the removed value is 'b'.
Key Takeaways
- 1Use pop when you know an index and need the removed value.
- 2Use del when you know an index and do not need a return value, or when you want to remove a range with a slice.
- 3Use remove when you know the value but not its index; it removes the first occurrence and returns None.
- 4Removing elements changes later indices because the remaining elements shift left.
- 5Avoid assigning del, do not expect remove to return the removed value, and consider missing values, invalid positions, and modification during iteration.
At a glance
- Choose between index-based removal and value-based removal first.
- pop removes by index and returns the removed element.
- del removes by index or slice and produces no value to assign.
- remove searches for and deletes the first occurrence of a value, returning None.
- After removal, later elements shift to new indices, so predict the list state after every operation.