Python - List remove() Method
The Python list 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.
In the below example, 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'] ValueError: list.remove(x): x not in list
❮ Python List Methods