Removing list items PREMIUM

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read 2 min. video Python 3.10—3.14
Python Morsels
Watch as video
02:17

Let's talk about the various ways to remove list items in Python.

Removing items by value

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.

Removing items from the end of a list

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.

Removing items by index

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']

Difference between pop and del

The 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.

Try to only remove items from the end of your lists

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.

Python Morsels
Watch as video
02:17
This is a free preview of a premium screencast. You have 2 previews remaining.