Skip to main content

How to Remove an Item from a List in Python with pop

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

list.pop() removes an item from a list and hands it back, so you can use what you took out. With no argument it takes the last item, which also makes it the way to undo the latest append(). This guide shows both forms and the error on an empty list, with real output from Python 3.15.2.

Syntax​

Syntax
<item> = <list>.pop()
<item> = <list>.pop(<index>)
PartWhat it means
<list>.pop()Removes and returns the last item
<list>.pop(<index>)Removes and returns the item at that position; 0 is the first
<item> = ...Optional: keep the removed item. The list changes either way

Python's tutorial: "Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. It raises an IndexError if the list is empty or the index is outside the list range."

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

pop_items.py
queue = ["first", "second", "third"]

last = queue.pop()
print(last)
print(queue)

first = queue.pop(0)
print(first)
print(queue)

The pop_items.py script in a macOS-style code window: a list of three strings, pop() keeps the removed last item, then pop(0) keeps the removed first item, printing the item and the list each time.

Step 2: Run It​

Terminal
uv run python pop_items.py
Output
third
['first', 'second']
first
['second']

The output in a terminal: third, then ['first', 'second'], then first, then ['second'].

pop() returned "third" and the list shrank to two items; pop(0) returned "first" and left one.

Popping an Empty List​

pop_empty.py
empty = []
empty.pop()
Terminal
uv run python pop_empty.py
Output
Traceback (most recent call last):
File "/Users/you/source/python-basics/pop_empty.py", line 2, in <module>
empty.pop()
~~~~~~~~~^^
IndexError: pop from empty list

Check that the list isn't empty first, for example with if empty:.

Common Mistakes​

MistakeWhat happensDo this instead
Popping an empty listIndexError: pop from empty listCheck the list first
Popping a position the list doesn't have, as in [1].pop(5)IndexError: pop index out of rangeUse an index between 0 and len(list) - 1
Expecting pop() to take the first itemIt takes the last oneUse pop(0) for the first

Troubleshooting​

SymptomLikely causeFix
A later loop misses itemsThe list was popped while a for loop was walking through itLoop over a copy, or build a new list

FAQ​

What's the difference between pop() and remove()?​

pop() removes by position and returns the item. remove() removes the first item equal to a value you give and returns nothing.

Can I undo an append() with it?​

Yes. append() adds at the end, and pop() removes from the end.

Conclusion​

queue.pop() took "third" off the end, queue.pop(0) took "first" off the front, both handed the item back, and an empty list raised IndexError: pop from empty list.

Additional Resources​