Cheap Jerseys Cheap NFL Jerseys Cheap NFL Jerseys Cheap Football jerseys Cheap NFL Jerseys Wholesale Jerseys Cheap NHL Jerseys Cheap Jerseys From China Cheap MLB Jerseys Cheap Jerseys Cheap Jerseys Cheap Jerseys Wholesale Jerseys Python List remove() Method Tutorial - WTMatter

Python List remove() Method Tutorial

Python List Remove() Method

This is a detailed tutorial of the Python List remove() method. Learn to remove a particular element from a Python List using with the help of examples.

Python List remove() Method

The remove() method is used to remove a particular item from a Python List. In case, the list contains multiple instances of the same item, meant to be removed, this method will only remove the first instance of the item. There’s no direct way to delete all the instances of a particular item directly using the method remove() in one go.

Syntax

Take any python list object and apply the remove method on it as illustrated in the following syntax.

remove() Parameters

As shown in the syntax, the Python List remove() method only takes one argument and that is the item that is to be removed from the list. The method will look up the list and the first instance of the item that matches this argument will be deleted or removed.

Note. In case, the provided argument does not match with any of the list items, i.e. if the item provided in the argument does not exists in the list, the list.remove() method will raise ValueError Exception like the one defined below.

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

remove() Return Value

This method does not return any value, i.e. it returns None type object.

Examples

Let’s have a look at the examples to better understand the usage of the list.remove() method.

Example 1. Removing an item from the List (Simplest Use-Case)

In the following example, we’re removing the element, Corona-Virus from a list of some earth-related items.

In the above, we’ve simply passed the string type item object "Corona-Virus" as the argument to the alist.remove() method and hence it has been removed from the list as shown in the output screenshot.

Python List Remove() Method Example

Example 2. remove() method applied on a List containing multiple instances of the item to be removed.

In the Python List here, we’ve two instances of the item "Common-cold", but as you can see in the screenshot of the program output, only the first instance of the item is deleted, while the second one, still remains in the list. Therefore, if you want to remove the second instance as well, you need to make use of the same list.remove(item) statement once more.

Python List Remove() Method Example For List Containing Multiple Item Instances

Example 3. The case when the item to be removed does not exist in the Python List

Now, here, we’re trying to remove the item "Problems" from our Python List that actually does not contain this item. Hence, this method is now raising the ValueError Exception as shown in the following screenshot of the program output.

Python List Remove() Method ValueError Exception Example

Note. In case, you just want to delete the last element of the list, you can make use of the pop() method instead of the remove() method. You also need not provide any argument to the pop() method because it already knows that it has to remove the item present at the end of the list.

There’s one more method to delete any item of the list using item indexes. This method is the use of the del keyword. For example, let’s say you have a list named fruits. Then del fruits[2] will delete or remove the third element or the item with the index 2 of the list.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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