A list is a container data type that represents a collection of ordered elements in which each elements has an index relative to other elements in the list. Lists are mutable meaning that operations such as insertions and removal can be done to a list in-place without creating a new list.

The remove() method removes the leftmost occurrence of an item from  a list.

ExampleEdit & Run
mylist = ['Python', 'Java', 'Python', 'C++']

#use the remove() method
mylist.remove('Python')

print(mylist)
Output:
['Java', 'Python', 'C++'][Finished in 0.01078653591684997s]
Syntax:
lst.remove(item)
item The item to be removed.

The remove method removes the first occurrence of the input value from the list and returns None. If the element is not in the list, a ValueError is raised.

ExampleEdit & Run
cities = ['Ottawa', 'Delhi', 'Tokyo', 'Denver']

cities.remove('Tokyo')
print(cities)
Output:
['Ottawa', 'Delhi', 'Denver'][Finished in 0.010191716952249408s]

As mentioned above, a ValueError is raised if the element does not exist in the list. 

ExampleEdit & Run
cities = ['Ottawa', 'Delhi', 'Tokyo', 'Denver']

#a ValueError is raised
cities.remove('Nairobi')

remove() method on a list with duplicate elements 

If the list contains duplicate elements, only the first matching element is removed.

ExampleEdit & Run
mylist = ['Tokyo', 'Beijing', 'Tokyo', 'Manila', 'Tokyo']

mylist.remove('Tokyo')

print(mylist)
Output:
['Beijing', 'Tokyo', 'Manila', 'Tokyo'][Finished in 0.00954484287649393s]

Remove all occurrences of an item from the list

We can call the remove() method repeatedly using a loop, to remove all the occurrences of an item from the list.

ExampleEdit & Run

Using a for loop

mylist = ['Java', 'Python', 'Java', 'C++', 'Python', 'Python']
item = 'Python'

for i in mylist:
    if i == item:
        mylist.remove(item)

print(mylist)
Output:
['Java', 'Java', 'C++', 'Python'][Finished in 0.009935597889125347s]

In the above example, the for loop iterates through the list elements. The if condition in the loop body checks whether the current element is equal to the element to be removed, if so, the remove() method is called. The net outcome is that all the occurrences of the item are removed from the list by the time the loop terminates.

We can also use a while loop together with the  the count() method  to achieve the same. The count() method returns the number of times that an element appears in a list,  we will use it to check whether the item exists before calling the remove() method.

ExampleEdit & Run
mylist = ['Python', 'Java', 'Python', 'C++', 'Python']

while mylist.count('Python'):
     mylist.remove('Python')

print(mylist)
Output:
['Java', 'C++'][Finished in 0.00929218390956521s]

In the above example, we used a while loop with a condition to check whether the element appears in the list or not. If the element's count is greater than 0, the loop body is executed and the leftmost element is removed, else, the condition fails and the loop terminates.

Alternatively, we can also catch the ValueError when it is raised using the try-catch statement instead of using the count() method. 

ExampleEdit & Run
mylist = ['a', 'a', 'b', 'c', 'a', 'd', 'a', 'a']

#remove all a's
try:
    while True:
         mylist.remove('a')        
except ValueError:
      pass

print(mylist)
Output:
['b', 'c', 'd'][Finished in 0.009419145993888378s]