Python List remove() Method
list.remove(value) deletes the first item equal to value, changes the list in place and returns None; it raises ValueError if the value is not in the list.remove() deletes by value, not by position. Use it when you know what you want to get rid of, such as taking "pen" out of a shopping cart, but you do not care or do not know where it sits. If you know the position instead, list.pop() or del my_list[i] is the better fit.
Only the first match is removed. If the same value appears several times, the later copies stay.
| Syntax | my_list.remove(value) |
|---|---|
| Parameters | value: the item to remove, compared with == |
| Returns | None (the list is modified in place) |
| Raises | ValueError if value is not in the list |
| Time complexity | O(n) |
How list.remove() finds and deletes an item
Python scans the list from the left, compares each element with value using ==, and deletes the first one that matches. Items after it shift one place left, so the list gets one element shorter.
cart = ["pen", "book", "pen", "lamp"]
cart.remove("pen")
print(cart)Removing a value that might not be there
Calling remove() with a missing value raises a ValueError. If the value may be absent, check first with in, or catch the error with try/except. Checking with in scans the list once more, which is fine for short lists.
Removing all occurrences, remove() vs pop() vs del
To remove every match, build a new list with a list comprehension such as [x for x in items if x != "pen"]. Never call remove() inside a for loop over the same list: deleting while iterating makes the loop skip items.
| Tool | Removes by | Returns | If missing / out of range |
|---|---|---|---|
remove(v) | Value (first match) | None | ValueError |
pop(i) | Index (default last) | The removed item | IndexError |
del a[i] | Index or slice | Nothing (statement) | IndexError for a single index |
clear() | Everything | None | Never fails |
Examples you can run
Press Run, then edit the code and run it again.
Remove one item by value
Only the first match is deleted.
marks = [40, 75, 40, 90]
marks.remove(40)
print(marks)Expected output[75, 40, 90]
Safe removal with a check
Test with in so a missing value does not crash the program.
tags = ["python", "web"]
for name in ["web", "sql"]:
if name in tags:
tags.remove(name)
print("removed", name)
else:
print(name, "not found")
print(tags)Expected outputremoved web
sql not found
['python']
Remove every occurrence
A comprehension keeps only the items you want.
cart = ["pen", "book", "pen", "lamp", "pen"]
cart = [item for item in cart if item != "pen"]
print(cart)Expected output['book', 'lamp']
Common mistakes
Removing a missing value
✗ Wrong
fruits = ["apple", "kiwi"]
fruits.remove("mango")✓ Right
fruits = ["apple", "kiwi"]
if "mango" in fruits:
fruits.remove("mango")
print(fruits)Check if "mango" in fruits: first, or wrap the call in try/except ValueError.
Removing 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 (nums[:]) or build a new list with a comprehension.
Using remove() with an index
✗ Wrong
items = ["a", "b", "c"]
items.remove(1)✓ Right
items = ["a", "b", "c"]
items.pop(1)
print(items)remove() takes a value. To delete by position use pop(1) or del items[1].
Python List remove() Method: questions people ask
How do I remove an item from a list in Python?
Use my_list.remove(value) to delete by value, my_list.pop(index) to delete by position, or del my_list[index]. All three change the list in place.
Does remove() delete all occurrences?
No, it deletes only the first match. Use a list comprehension such as [x for x in a if x != v] to drop every occurrence.
What does list.remove() return?
It returns None. If you want the removed item back, use pop() instead.
Keep learning
Step-by-step lessons:
Python ListsPython List Methods
Related:
Python List pop() MethodPython List clear() MethodPython List index() MethodPython List count() MethodPython List Methods
More in list methods:
Python List append() MethodPython List extend() MethodPython List insert() MethodPython List pop() MethodPython List clear() MethodPython List index() MethodPython List count() MethodPython List sort() Method See all →