Python List remove()

In this tutorial, we will see about Python List‘s remove method.Python List remove method is used to remove passed element from the list.It will remove first matching element from the list.

Python List remove syntax

here list1 is object of list.

Python List remove example

You can simply call remove method to delete an element from the list.
Let’s understand this with the help of simple example.

Output:

listOfCar: [‘Honda’, ‘Hyundai’, ‘Suzuki’]

As you can see here, ‘Maruti’ got removed from the list.
What if passed element is not in the list?
If element is not in the list, then remove method will raise ValueError.

Output:

—————————————————————————
ValueError Traceback (most recent call last)
in ()
1 listOfCars=[‘Honda’,’Hyundai’,’Maruti’,’Suzuki’] —-> 2 listOfCars.remove(‘Punto’)
3 print(“listOfCar:”,listOfCars)

ValueError: list.remove(x): x not in list

That’s all about Python List remove method. Also, check how to remove last element from list in python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *