Python List remove() - Removes Element From List
The remove()
removes the first occurance of the specified item from the list. It raises a ValueError
if item does not exist in a list.
Syntax:
list.remove(item)
Parameters:
item: (Required) The element to removed from the list.
Return Value:
No return value.
The following example demonstrates the remove()
method.
cities = ['Mumbai', 'London', 'Paris', 'New York']
cities.remove('Mumbai')
print("Updated List: ",cities)
cities.remove('Paris')
print("Updated List: ",cities)
Updated List: ['London', 'Paris', 'New York']
Updated List: ['London', 'New York']
The remove()
method is case-sensitive.
cities = ['Mumbai', 'London', 'Paris', 'New York']
cities.remove('mumbai') # raise an error, can't find 'mumbai''
print("Updated List: ",cities)
Traceback (most recent call last):
cities.remove('mumbai')
ValueError: list.remove(x): x not in list
The list.remove() function also works on integer lists.
numbers = [1, 2, 3, 4, 5, 6]
numbers.remove(5)
print(numbers)
[1, 2, 3, 4, 6]