If you want to delete an item from the list in Python, both remove()
and pop()
methods might come in handy - but they do have some differences.
In this post, we'll compare these methods and show you how to use them in Python!
The remove()
method
This method removes the first matching value from the list. The first matching value is found by iterating over the list until the given value is equal (==
) to the value of the item in the list.
Here's how to use the remove()
method in Python:
customers = [
['John', 1],
['John', 1],
['Lisa', 2],
['Rolf', 3],
['John', 4]
]
customers.remove(['John', 1])
If you run this code, the customers
variable will look like this:
customers = [
['John', 1],
['Lisa', 2],
['Rolf', 3],
['John', 4]
]
That is because only the first matching value was removed from the list.
In this case, the remove()
method is useful if you want to remove a customer with a given name and id, without the need to search through the list yourself. You can use it in other cases where you know the value but not the exact index of the item you want to remove.
If the provided value is not in the list, then a ValueError
will be raised.
The pop()
method
The pop()
method is used to remove an item from the list and return it.
Removing items from the list one by one and retrieving their value can be useful. You might want to process the removed data, save it somewhere else, etc. In Python, we do it using the pop()
method!
If you use pop()
without any arguments, the last item will be removed from the list and returned by default:
customers = [
['John', 1],
['Lisa', 2],
['Rolf', 3],
['John', 4]
]
customer = customers.pop() # ['John', 4]
The customers
list is changed to:
customers = [
['John', 1],
['Lisa', 2],
['Rolf', 3]
]
And the customer
variable now looks like this:
customer = ['John', 4]
This method can also take an argument, which is the index of the item that you want to remove:
customers.pop(1)
In this case, the second item will be removed from the list and returned. Remember that you can assign the return value to a variable.
Finally, the customers
variable now looks like this:
customers = [
['John', 1],
['Rolf', 3]
]
Removing items from nested lists
You might encounter a list that looks like this:
customers = [
[["John", 11],["Lisa", 12],["Rolf", 13]],
[["Murilo", 21],["Joshua", 22]]
]
Customers might have been grouped by the city that they come from, country, etc.
A higher level of nesting requires you to be precise when using the above methods, which we will cover below.
Use remove()
with double-nested lists
Let's try to remove ["John", 11]
from the customers
list the same way as we did before:
customers.remove(["John", 11])
If you run this code, you will be greeted with a ValueError
. The error will occur because ["John", 11]
is an item of a nested list that is an item inside the bigger list itself, as shown in the following diagram:
To remove this item, you will need to access the inner list first and then remove it:
customers[0].remove(["John", 11])
In this line, we've accessed the first item of the list, which is the inner list, and then removed the first matching value. The changed list looks like this:
customers = [
[["Lisa", 12],["Rolf", 13]],
[["Murilo", 21],["Joshua", 22]]
]
You can also remove the whole inner list if you want to remove a whole group of customers for example.
customers.remove([["Lisa", 12],["Rolf", 13]])
In this case, you don't have to access any item of the main list since you are removing its item in the same way as we did in the first part of the post.
Use pop()
with double-nested lists
We can now remove an item from the list using the pop()
method:
customers = [
[["John", 11],["Lisa", 12],["Rolf", 13]],
[["Murilo", 21],["Joshua", 22]]
]
customer = customers.pop() # [["Murilo", 21],["Joshua", 22]]
The customers
list will now look like this:
customers = [[["John", 11],["Lisa", 12],["Rolf", 13]]]
The reason for this is that we applied the method on a whole customers
list, whose items are the inner lists.
You can also remove items from the inner nested lists using the pop()
method:
customers[0].pop(1)
We are now applying the method on the first item of the customers
list:
[["John", 11],["Lisa", 12],["Rolf", 13]]
Which removes its second item: ["Lisa", 12]
. After that, the customers
list becomes:
customers = [[["John", 11],["Rolf", 13]]]
Conclusion
In this post we've seen two ways of removing items from a Python list: by using the remove()
and pop()
methods. Both of these methods can be useful in different situations, so we encourage you to try them out yourself!
If you want to learn more about Python, consider enrolling in our Complete Python Course which takes you from beginner all the way to advanced (including OOP, web development, async development, and much more!). We have a 30-day money-back guarantee, so you really have nothing to lose by giving it a try. We'd love to have you!