Python 3 reference · List Methods
Python List remove() Method
Remove the first item equal to a value. Raises ValueError if the value is not in the list.
Python 3 Since 3.0
Syntax
list.remove(value)
Parameters
| Parameter | Description |
|---|---|
value |
The value to remove. |
Return value
None.
Examples
pets = ["cat", "dog", "cat"]
pets.remove("cat")
print(pets)
Output
['dog', 'cat']
nums = [1, 2, 1, 3, 1]
nums = [n for n in nums if n != 1] # remove every match
print(nums)
Output
[2, 3]
Don't remove items from a list while looping over it; build a new list with a comprehension instead.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
list.pop()· Remove and return the item at a position, the last item by default. Raises IndexError on an empty list.list.clear()· Remove every item from the list in place, leaving an empty list. Other references see the change too.set.discard()· Remove an element if it is present; do nothing if it isn't. The no-error version of remove().- Python Lists · tutorial