AppDividend
Latest Code Tutorials

Python List Remove Example | remove() Method Tutorial

0 10

Get real time updates directly on you device, subscribe now.

Python List Remove Example | remove() Method Tutorial is today’s topic. The remove() method searches for the given item in the list and removes the first matching item. Python list remove() method does not return any value but removes the given object from the list.

Python List Remove Example

The syntax of list remove() method is following.

list.remove(item)

The remove() method takes a single element as an argument and removes it from the list.

The item parameter is required and any type (string, number, list) the element you want to remove. The remove() method only removes the given element from the list. It doesn’t return any value.

Let us take a simple example.

// app.py

GoT = ['Daenerys', 'Jon', 'Tyrion']
GoT.remove('Jon')
print(GoT)

In the above example, we are removing the Jon element.

 

Python List Remove Example | remove() Method Tutorial

Trying to Delete Element That Doesn’t Exist

Let us remove an item which does not exist in the list and see what we get in the output.

// app.py

GoT = ['Daenerys', 'Jon', 'Tyrion']
GoT.remove('Arya')
print(GoT)

 

Trying to Delete Element That Doesn't Exist

If the element(argument) passed to the remove() method doesn’t exist, valueError exception is thrown. So, the list remove() method removes the element which is passed as an argument.

Finally, Python List Remove Example | remove() Method Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.