table of Contents
There are multiple elements in the list to be deleted
description
The list.remove() method removes the element of the specified value in the list. Commonly used in scenarios where you are uncertain or don't care about the position of an element in the list.
Syntax and parameters
list.remove(object)
name | meaning | Remarks |
object | The element to be removed from the list | Parameters that cannot be omitted |
return value
None
Usage example
>>> demo = [1, 3, 5]
>>> demo.remove(5)
>>> demo
[1, 3]
Precautions
Delete non-existent elements
When deleting an element that does not exist in the list, Python throws a ValueError exception.
>>> demo = [1, 3, 5]
>>> demo.remove(6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
There are multiple elements in the list to be deleted
When there are more than one element to be deleted in the list, the remove() method deletes the first matching element.
demo = ["Pod", "ReplicationController", "Namespace", "ReplicationController"]
demo.remove("ReplicationController")
print(demo)
# outputs:
# ['Pod', 'Namespace', 'ReplicationController']