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.

Example:
cities = ['Mumbai', 'London', 'Paris', 'New York']
cities.remove('Mumbai')
print("Updated List: ",cities)
cities.remove('Paris')
print("Updated List: ",cities)
Output
Updated List:  ['London', 'Paris', 'New York']
Updated List:  ['London', 'New York']

The remove() method is case-sensitive.

Example:
cities = ['Mumbai', 'London', 'Paris', 'New York']
cities.remove('mumbai') # raise an error, can't find 'mumbai''
print("Updated List: ",cities)
Output
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.

Example:
numbers = [1, 2, 3, 4, 5, 6]
numbers.remove(5)
print(numbers)
Output
[1, 2, 3, 4, 6]
Want to check how much you know Python?