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
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 |
This method doesn't return any value. It modifies the original list by removing elements from it.
# 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']
# 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']
# 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, inflowers.remove('jasmine') ValueError: list.remove(x): x not in list