Tutorial Study Image

Python remove()

The remove() function in python helps to remove the given element from the list. If the list having more than one matching element it removes the first occurrence only.


list.remove(element) #where element may be string, number, list etc
 

remove() Parameters:

The remove() function takes a single parameter. If the given element is not found in the list it will raise a ValueError

Parameter Description Required / Optional
element The element you want to remove Required

remove() Return Value

This method doesn't return any value. It modifies the original list by removing elements from it.

Examples of remove() method in Python

Example 1:How to remove element from the list


# flowers list
flowers = ['Dalia', 'Rose', 'Lotus', 'Sunflower']

# 'Sunflower' is removed
flowers.remove('Sunflower')

# Updated flowers List
print('Updated flowers list: ', flowers)
 

Output:


Updated flowers list:['Dalia', 'Rose', 'Lotus']

Example 2:How to use remove() method on a list having duplicate elements?


# flowers list
flowers = ['Dalia', 'Rose', 'Lotus', 'Sunflower', 'Rose']

# 'Rose' is removed
flowers.remove('Rose')

# Updated flowers List
print('Updated flowers list: ', flowers)
 

Output:


Updated flowers list: ['Dalia', 'Lotus', 'Sunflower', 'Rose']

Example 2:How remove() method works in missing element?


# flowers list
flowers = ['Dalia', 'Rose', 'Lotus', 'Sunflower']

# Deleting 'jasmine' element
flowers.remove('jasmine')

# Updated flowers List
print('Updated flowers list: ', flowers)
 

Output:


Traceback (most recent call last):
  File ".. .. ..", line 5, in 
    flowers.remove('jasmine')
ValueError: list.remove(x): x not in list