PythonPython ListsList removal methods comparison
What is the difference between `remove()`, `pop()`, and `del`?
Question 84 of 88 · Updated
On this page
Use remove() when you know the value, pop() when you want to remove and retrieve an item by position, and del when you want to delete an item or slice by position without retrieving it. All three modify the original list.
Quick comparison#
| Feature | remove(value) | pop(index) | del items[index] |
|---|---|---|---|
| Selects by | Value (first match) | Index (last by default) | Index or slice |
| Returns | None | Removed item | No return value (statement) |
| Missing value or invalid index | ValueError | IndexError | IndexError for a single index |
| Best use | Cancel a known item | Take an item to process | Discard unwanted positions |
Practical example: Manage a task list#
PYTHONtasks = ['email', 'meeting', 'report', 'email'] tasks.remove('email') # ['meeting', 'report', 'email'] done = tasks.pop() print(done) # email del tasks[0] print(tasks) # ['report']
Here remove() cancels the first matching task, pop() retrieves the last task for processing, and del discards the meeting at index 0.
Delete multiple items with del#
Unlike pop(), del supports slices. Starting with items = [10, 20, 30, 40], executing del items[1:3] leaves [10, 40]. Slice bounds are clipped to the list length, so an out-of-range slice does not normally raise IndexError.
Common mistakes#
items.remove(2)searches for the value 2, not index 2.- Calling pop() on an empty list raises IndexError.
- Do not assign the result of remove() back to your list: it returns None.
- del is a statement, not a list method.
Try it: Given items = [10, 20, 30], which operation removes and returns 20? Answer: items.pop(1).
Interview takeaway: Choose according to what you know (value or index) and whether you need the removed object. In CPython, removing near the front can take O(n) time because later references shift; popping the last item is amortized O(1).
Official references#
References
- 1.Python Tutorial — More on Listsdocs.python.org
- 2.Mutable sequence typesdocs.python.org
Related questions
Explore other topics:Forward Deployed EngineeringMachine LearningSQL: Structured Query LanguageAI System Design