Sign in to your Python Morsels account to save your screencast settings.
Don't have an account yet? Sign up here.
Let's talk about the various ways to remove list items in Python.
When learning about lists, folks often stumble upon the list remove method.
The remove method removes an item based on its value:
>>> fruits = ["apples", "bananas", "watermelons", "bananas", "pears"]
>>> fruits.remove("bananas")
Note that the remove method only removes the first occurrence of an item.
So bananas was in this list twice, and now it's only in the list once:
>>> fruits
['apples', 'watermelons', 'bananas', 'pears']
I very rarely use the list remove method.
Usually, when I want to remove an item from a list, I want to remove it based on its position, instead of its value.
To remove an item from the end of a list, we can use the pop method:
>>> fruits
['apples', 'watermelons', 'bananas', 'pears']
>>> fruits.pop()
'pears'
>>> fruits
['apples', 'watermelons', 'bananas']
You can think of the pop method as the opposite of the append method.
Just as the append method adds an item to the end of a list, the pop method removes an item from the end of a list:
>>> fruits.append("oranges")
>>> fruits.pop()
'oranges'
>>> fruits
['apples', 'watermelons', 'bananas']
Note that the pop method also returns the removed item:
>>> fruits.pop()
'bananas'
So we can both remove an item, and see which item we removed, in just one method call.
By default, the pop method removes an item from the end of a list.
But pop can also remove items from anywhere else in a list.
If we pass 0 to the pop method, it will remove the first item from the list (i.e. the item that was at index 0):
>>> fruits = ["apples", "pears", "bananas", "watermelons"]
>>> fruits.pop(0)
'apples'
>>> fruits
['pears', 'bananas', 'watermelons']
The pop method accepts an optional index to pop from.
Index 0 represents the first item in the list, but any index will work.
For example, we could pop from index -2 to remove the second-to-last item from the list:
>>> fruits.pop(-2)
'bananas'
>>> fruits
['pears', 'watermelons']
pop and delThe pop method isn't the only way to remove an item based on its index.
The del statement can also do this:
>>> fruits = ["apples", "pears", "bananas", "watermelons"]
>>> del fruits[-1]
>>> fruits
['apples', 'pears', 'bananas']
Note that unlike the pop method, the del statement doesn't return the removed item to us.
In fact, since del is a statement, it can't be embedded within another line of code:
>>> removed = del fruits[0]
File "<python-input-10>", line 1
removed = del fruits[0]
^^^
SyntaxError: invalid syntax
Statements, unlike other Python expressions, must be a line of code all on their own.
The del statement is lower level than the pop method, but I don't see it used as often.
The most common way to remove an item from a list is to use the pop method, usually to remove the last item.
It's worth noting that using the pop method or the del statement to remove the last item is more efficient than removing the first item.
I talk about that more in my video on the list insert method.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces.
Sign in to your Python Morsels account to track your progress.
Don't have an account yet? Sign up here.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces. Learn to avoid beginner pitfalls, in less than a week!
Ready to level up? Sign up now to begin your Python journey the right way!