remove

list.pop

Removes and returns the item at position i (default -1, the last item). Raises IndexError on an empty list or an out-of-range index.

list.pop([i])

Parameters

Parameter Purpose
i (optional) Index to pop; defaults to -1 (last element). Accepts negative indices.
return value Unlike most mutators, pop returns the removed element — assignable.

Examples

>>> lst = [1, 2, 3]
>>> lst.pop()
3
>>> lst
[1, 2]

Default pops and returns the last element.

>>> lst = [1, 2, 3]
>>> lst.pop(0)
1
>>> lst
[2, 3]

pop(0) is O(n) — use collections.deque.popleft() for queues.

>>> [].pop()
IndexError: pop from empty list

Empty list raises IndexError.

>>> lst = [1, 2, 3]
>>> lst.pop(-2)
2

Negative indices allowed.

Gotcha

pop() is the one common list mutator that RETURNS the removed value (not None). pop(0) is O(n); prefer collections.deque for FIFO patterns.

Related methods

← All Python list methods · Dict methods · list vs tuple vs set