Python List - remove() Method
The Python remove() method is used to delete the first occurrence of a specified element from the list. If specified element does not exist in the list, the method will raise an exception.
Syntax
list.remove(elem)
Parameters
elem |
Required. value of the element which need to be removed from the list. |
Return Value
None.
Example:
In the example below, list remove() method is used to delete the first occurrence of element called 'MAR' from the list called MyList. When an element called 'DEC' is attemped to delete from MyList which is not present in the list, the method raises an exception.
MyList = ['JAN', 'FEB', 'MAR', 'MAR', 'APR'] MyList.remove('MAR') print(MyList) MyList.remove('DEC') print(MyList)
The output of the above code will be:
['JAN', 'FEB', 'MAR', 'APR'] Traceback (most recent call last): File "Main.py", line 6, in <module> MyList.remove('DEC') ValueError: list.remove(x): x not in list
❮ Python List Methods