remove() method in Python Lists can be used to remove a given element from the list.
The remove() method in Python Lists returns ‘None‘ as a value, however, modifies the original string by removing the element.
Syntax
list.remove(item)
item: It is the element that needs to be removed. (mandatory)
l = [1, 2, 3, 4, 5, 6]
print(l.remove(4))
print(l)
O/P
None
[1, 2, 3, 5, 6]
Please note that remove() method only removes the first occurrence of the specified element.
l = [1, 2, 3, 2, 4, 2, 2, 5]
l.remove(2)
print(l)
O/P
[1, 3, 2, 4, 2, 2, 5]
If specified element does not exists in Python List, so remove() method returns ValueError.
l = [1, 2, 3, 5]
l.remove(10)
print(l)
O/P
Traceback (most recent call last):
File “”, line 2, in
ValueError: list.remove(x): x not in list
You can also use the Python del statement to remove items from the list.
That’s all here.
