Python basics

Python list pop(): Remove by Index and Handle Empty Lists Safely

Python basics · · Published 2026-12-04

Stacks of black notebooks on a table, with evergreen branches beside them
Stacked notebooks work like pop() with no argument: the last one added is the first one taken. Photo: Courtney Robertson, WordPress Photo Directory (CC0).

list.pop() removes an item from a Python list and gives it back to you in the same step. Without an argument it takes the last item; with an index it takes the item at that position. It is the method behind every stack you will write in Python, and it has one behaviour that trips up beginners: on an empty list it raises an IndexError instead of returning None. This guide covers the syntax, the error and the three usual ways to deal with it, how pop() differs from remove() and del, and the cost of popping from the front of a long list.

Every example below was run on Python 3.13; the outputs are copied from the terminal. pop() has worked this way in every version of Python 3.

The syntax in one table

CallRemovesReturnsIf it cannot
lst.pop()the last itemthat itemIndexError: pop from empty list
lst.pop(i)the item at index ithat itemIndexError: pop index out of range
lst.pop(-2)the second item from the endthat itemsame error

The Python tutorial describes the method as removing "the item at the given position in the list, and return it", with the last item as the default. The list is changed in place: there is no copy.

Removing the last item

tasks = ["write", "test", "review", "deploy"]
last = tasks.pop()
print(last)
print(tasks)
deploy
['write', 'test', 'review']

This is the form you will use most. Combined with append(), it turns any list into a stack: append pushes an item on top, pop takes the top item off. Undo histories, the "back" button of a browser and the depth-first walk of a tree are all built this way. Our comparison of append, extend and insert covers the other half of that pattern.

Removing an item by index

Pass a position to remove something other than the last item. Negative indexes count from the end, exactly as they do with square brackets.

first = tasks.pop(0)
print(first, tasks)

nums = [10, 20, 30, 40]
print(nums.pop(-2), nums)
write ['test', 'review']
30 [10, 20, 40]

Notice that pop(i) takes a position, not a value. To remove the value 30 wherever it is, you want remove(30), covered below.

The empty-list error, and three ways to handle it

Popping from an empty list, or with an index that does not exist, raises IndexError:

IndexError: pop from empty list
IndexError: pop index out of range

The message tells you which case you hit. There are three common ways to avoid it, and the right one depends on whether an empty list is normal in your program or a sign that something went wrong.

1. Check before popping

stack = []
item = stack.pop() if stack else None
print(item)
None

An empty list is "falsy", so if stack is the idiomatic test. Use this when running out of items is expected, for example when a loop consumes a work queue until nothing is left: while stack: item = stack.pop().

2. Catch the exception

try:
    item = stack.pop()
except IndexError:
    item = None

This style is common when the list is usually full and an empty one is the exception. Python's documentation calls it "easier to ask for forgiveness than permission" (EAFP), the opposite of "look before you leap" (LBYL), in its glossary. Keep the try block short: wrapping a whole function in it can hide an IndexError that comes from somewhere else.

3. Let it fail

If the list should never be empty at that point, do nothing. The traceback points straight at the bug, which is better than silently carrying a None into the rest of the program.

One thing does not exist: lists have no default argument. dict.pop(key, default) accepts a fallback value, which is why people expect the same from lists, but lst.pop(0, None) raises a TypeError.

d = {"a": 1}
print(d.pop("b", "missing"))
missing

pop() vs remove() vs del

ToolYou give itReturnsError when missing
lst.pop(i)a position (default: last)the removed itemIndexError
lst.remove(x)a value (first match)NoneValueError
del lst[i]a position or a slicenothing (statement)IndexError
s = [3, 1, 2]
r = s.remove(1)
print(r, s)

x = [1, 2, 3]
del x[0]
print(x)
None [3, 2]
[2, 3]

Rule of thumb: use pop when you need the removed item, remove when you know the value but not its position, and del when you want to drop a position or a whole slice (del lst[2:5]) and do not need anything back. The mutable sequence types table of the documentation lists all three operations.

Why pop(0) is slow, and what to use instead

A Python list stores its items in one contiguous block. Removing the last item only shortens the list. Removing the first one forces every other item to move one slot to the left. The collections documentation says it plainly: lists "incur O(n) memory movement costs for pop(0) and insert(0, v) operations".

We measured it with timeit on Python 3.13 (100,000 pops from a list of 200,000 integers, same laptop, 4 December 2026):

OperationTime for 100,000 calls
lst.pop()about 2 ms
lst.pop(0)about 2,400 ms
deque.popleft()about 2 ms

The gap is a factor of about a thousand, and it grows with the length of the list. If you need a queue (first in, first out), use collections.deque, which the same page describes as supporting appends and pops from either side "with approximately the same O(1) performance in either direction":

from collections import deque
q = deque(["a", "b", "c"])
print(q.popleft(), q)
a deque(['b', 'c'])

For a short list, a few dozen items, pop(0) is fine and simpler to read. The cost only matters in loops over long lists.

Two more pitfalls

For the bigger picture, start from our guide to Python lists; to count what is left in a list after removing items, see counting occurrences in a list.