Python List | remove method
Start your free 7-days trial now!
List.remove(~) method removes the first occurrence of the element with the specified value.
The element to be removed from the list.
# List of numbersnumbers = [1,2,2,3,4]# Remove first occurrence of 2 from numbersnumbers.remove(2)print(numbers)[1, 2, 3, 4]
Notice that there is now only one occurrence of 2 in numbers now.
If the item to remove does not exist in the list then ValueError is returned.
# List of numbersnumbers = [1,2,2,3,4]# Remove first occurrence of 5 from numbersnumbers.remove(5)print(numbers)ValueError: list.remove(x): x not in list