Python remove() vs pop() vs del: What Is the Difference?
remove(x) deletes the first item equal to a value, pop(i) deletes the item at an index and hands it back, and del is a statement that deletes by index, slice or name and returns nothing.All three tools take something out of a collection, so beginners often grab whichever one they remember first. The right choice depends on what you know about the item: do you know its value, its position, or do you need it afterwards? Picking correctly avoids ValueError, IndexError and subtle bugs where the wrong element disappears.
This page focuses on Python lists, where all three work, and also shows how they behave on dictionaries. For the full method references see list.remove() and list.pop().
remove() vs pop() vs del at a glance
| Aspect | `list.remove(x)` | `list.pop(i)` | `del` |
|---|---|---|---|
| What you give it | A value | An index (default: last item) | An index, a slice or a name |
| What it removes | The first item equal to the value | The item at that position | Whatever the target refers to |
| Return value | None | The removed item | Nothing: it is a statement, not a function |
| Works on slices | No | No | Yes: del nums[1:3] |
| Missing target | ValueError | IndexError (also on an empty list) | IndexError, KeyError or NameError |
| Removes several items at once | No, only the first match | No | Yes, with a slice |
| Typical use | Delete a known value | Take an item and use it (stack, queue) | Delete a range or free a variable |
When to use remove()
Use remove() when you know what you want gone but not where it sits, for example removing a product from a cart by name. It scans from the left and deletes only the first match, so duplicates later in the list stay. It raises ValueError if the value is absent, so check with in first when the value might not be there.
cart = ["pen", "book", "pen"]
if "lamp" in cart:
cart.remove("lamp")
cart.remove("pen")
print(cart)If you need to delete every occurrence, build a new list with a list comprehension instead of calling remove() repeatedly.
When to use pop()
Use pop() when you need the removed item afterwards. With no argument it takes the last element in O(1) time, which is why a list works well as a stack. pop(0) takes the first element but must shift every other item, so it is O(n); for large queues prefer collections.deque. On a dictionary, pop(key) removes the key and returns its value, and accepts a default so a missing key does not raise (see dict.pop()).
When to use the del statement
Use del when you do not need the item back, or when you want to remove a slice. It is also the only one of the three that can delete a variable name or an entry from a dictionary by key without needing a return value. Because del is a statement, you write del nums[0], not del(nums[0]) as a call that returns something (the parentheses are allowed but only group the expression).
One more subtlety: del nums removes the name nums, not the list's items. Use nums.clear() to empty a list but keep the name. Deleting a name also does not destroy the object if another name still refers to it; see Python variables for how names and objects relate.
Examples you can run
Press Run, then edit the code and run it again.
remove() deletes the first matching value
Only the first "pen" goes; the second one is kept, and nothing is returned.
cart = ["pen", "book", "pen", "lamp"]
result = cart.remove("pen")
print(cart)
print(result)Expected output['book', 'pen', 'lamp']
None
pop() removes by index and returns the item
pop() with no argument takes the last item; pop(0) takes the first.
scores = [70, 85, 92]
last = scores.pop()
first = scores.pop(0)
print(last, first, scores)Expected output92 70 [85]
del removes an index or a whole slice
A slice 1:3 removes indexes 1 and 2 in one step.
nums = [0, 1, 2, 3, 4, 5]
del nums[1:3]
print(nums)
del nums[0]
print(nums)Expected output[0, 3, 4, 5]
[3, 4, 5]
Deleting from a dictionary with del and pop
del just removes the key. pop() also returns the value, and a second argument is a safe default.
prices = {"tea": 3, "coffee": 4}
del prices["tea"]
value = prices.pop("coffee")
print(value, prices)
print(prices.pop("juice", 0))Expected output4 {}
0
Common mistakes
Removing a value that is not in the list
✗ Wrong
cart = ["pen", "book"]
cart.remove("lamp")✓ Right
cart = ["pen", "book"]
if "lamp" in cart:
cart.remove("lamp")
print(cart)Check membership first with in, or catch ValueError if a missing item is expected.
Popping from an empty list
✗ Wrong
stack = []
stack.pop()✓ Right
stack = []
if stack:
stack.pop()
print(stack)Test the list first: an empty list is falsy, so if stack: is enough.
Removing items while looping over the same list
✗ Wrong
nums = [1, 2, 2, 3]
for n in nums:
if n == 2:
nums.remove(n)
print(nums)✓ Right
nums = [1, 2, 2, 3]
nums = [n for n in nums if n != 2]
print(nums)Loop over a copy, or build a new list with a comprehension.
Using del on the name instead of the items
✗ Wrong
items = [1, 2, 3]
del items
print(items)✓ Right
items = [1, 2, 3]
items.clear()
print(items)del items deletes the variable. Use items.clear() to empty the list.
Python remove() vs pop() vs del: What Is the Difference?: questions people ask
What is the difference between remove() and pop() in Python?
remove(x) deletes the first item equal to a value and returns None, while pop(i) deletes the item at an index and returns that item. Use remove() when you know the value and pop() when you know the position or need the item back.
Is del faster than pop() or remove()?
Deleting from the end of a list is O(1) for both del nums[-1] and nums.pop(), so the speed is practically the same. Removing from the front or middle is O(n) for all three because later items must shift, and remove() also has to search first.
How do I remove all occurrences of a value from a list?
Build a new list that keeps everything else, for example [n for n in nums if n != 2]. Calling remove() removes only the first match each time.
Keep learning
Step-by-step lessons:
Python List MethodsPython Lists
Related:
Python List remove() MethodPython List pop() MethodPython Dictionary pop() MethodPython List clear() MethodPython append() vs extend(): What Is the Difference?
More in functions and methods:
Python append() vs extend(): What Is the Difference?Python sort() vs sorted(): What Is the Difference?Python print() vs return: What Is the Difference?Python str() vs repr(): What Is the Difference?Python Deep Copy vs Shallow Copy: What Is the Difference? See all →