Python List remove()

The remove() method searches for the given element in the list and removes the first matching element.

The syntax of remove() method is:

list.remove(element)

remove() Parameters

The remove() method takes a single element as an argument and removes it from the list.

If the element(argument) passed to the remove() method doesn't exist, valueError exception is thrown.


Return Value from remove()

The remove() method only removes the given element from the list. It doesn't return any value.


Example 1: Remove Element From The List

# animal list
animal = ['cat', 'dog', 'rabbit', 'guinea pig']

# 'rabbit' element is removed
animal.remove('rabbit')

#Updated Animal List
print('Updated animal list: ', animal)

When you run the program, the output will be:

Updated animal list:  ['cat', 'dog', 'guinea pig']

Example 2: remove() Method on a List having Duplicate Elements

# If a list contains duplicate elements
# the remove() method removes only the first instance

# animal list
animal = ['cat', 'dog', 'dog', 'guinea pig', 'dog']

# 'dog' element is removed
animal.remove('dog')

#Updated Animal List
print('Updated animal list: ', animal)

When you run the program, the output will be:

Updated animal list:  ['cat', 'dog', 'guinea pig', 'dog']

Here, only the first occurrence of element dog is removed from the list.


Example 3: Trying to Delete Element That Doesn't Exist

# animal list
animal = ['cat', 'dog', 'rabbit', 'guinea pig']

# Deleting 'fish' element
animal.remove('fish')

# Updated Animal List
print('Updated animal list: ', animal)

When you run the program, you will get the following error:

Traceback (most recent call last):
  File ".. .. ..", line 5, in <module>
    animal.remove('fish')
ValueError: list.remove(x): x not in list

It's because the element fish doesn't exist in the animal list.


The remove() method removes the element which is passed as an argument.

However, if you need to delete elements based on index (like fourth element or last element), you need to use either pop() method or del operator.

This function is also used for following native datatypes: