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.
Table of Contents
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.
1 |
list.remove(itemToBeRemoved) |
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.
1 2 3 4 5 6 7 8 9 10 11 |
#List containing some Items alist = ["Humans","Animals","Economies","Corona-Virus","Unity"] #Current Items print(alist) #Remove an item from the List alist.remove("Corona-Virus") #Updated List after removing the item print(alist) |
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.
Example 2. remove() method applied on a List containing multiple instances of the item to be removed.
1 2 3 4 5 6 7 8 9 10 11 12 |
#List containing duplicate items alist = ["Humans","Animals","Economies","Common-Cold","Unity","Common-Cold"] #Current Items print(alist) #Remove an item from the List #only the first instance of the item will be removed alist.remove("Common-Cold") #Updated List after removing the item print(alist) |
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.
Example 3. The case when the item to be removed does not exist in the Python List
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#ValueError: list.remove(x): x not in list Example #List containing items alist = ["Humans","Animals","Economies","Unity"] #Current Items print(alist) #Remove an item that is not contained in the list alist.remove("Problems") #Updated List after removing the item print(alist) print("No More Problems!") |
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.
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!