The remove()
method in Python is a List method that removes the first occurrence of a specified value from a list. If the value is not found in the list, it will raise a ValueError. It does not return any value and only modifies the original list in place.
Parameter Values
Parameter | Description |
---|---|
value | The value to be removed from the list. |
Return Values
The remove()
method returns None
as it modifies the list in-place.
How to Use remove()
in Python
Example 1:
The remove()
method removes the first occurrence of a specified value from a list.
numbers = [1, 2, 3, 4, 3, 5]
numbers.remove(3)
print(numbers) # Output: [1, 2, 4, 3, 5]
Example 2:
If the specified value is not found in the list, a ValueError is raised.
fruits = ['apple', 'banana', 'orange']
fruits.remove('grape') # ValueError: list.remove(x): x not in list
Example 3:
The remove()
method only removes the first occurrence of the specified value.
letters = ['a', 'b', 'c', 'b', 'd']
letters.remove('b')
print(letters) # Output: ['a', 'c', 'b', 'd']