pop()removes an item and returns it in one step. With no argument it takes the last item; pop(i) takes the item at index i, and the items after it shift left.
Negative indexes count from the end, so pop(-2) takes the second to last item. Popping from an empty list, or with an index outside the list, raises IndexError.
list.pop() is an instance method of the built-in list type: call it on a list with dot notation, such as history.pop(...). No import is needed.
Syntax
Leave the parentheses empty for the last item, or pass a position; keep the returned item if you need it.
python
item = lst.pop()
item = lst.pop(index)
Call
What it does
[1, 2, 3].pop()
Returns 3; the list becomes [1, 2].
["a", "b", "c"].pop(0)
Returns 'a'; the list becomes ['b', 'c'].
[].pop()
Raises IndexError: there is nothing to remove.
Popping the last item is fast; pop(0) has to shift every other item, so it slows down on long lists, and collections.deque.popleft() is the usual choice for big queues. To delete by value use remove(); to delete by position without keeping the item, del lst[i] also works.
Method signatures
python
list.pop(index=-1)
# returns: the removed item
# raises: IndexError if the list is empty or index is out of range
Parameters
Parameter
Description
index
Optional integer position, default -1 (the last item). Negative values count from the end. It must be a position, not a value: lst.pop("x") raises TypeError.
Return Value
Return Value
Description
the removed item
The object that was stored at index. The list is now one item shorter.
Python Examples
Example 1 — Undo the Latest Edits in a Design Tool
Push each edit with append() and undo with pop(), newest first.
python
history = []
history.append("type title")
history.append("add image")
history.append("change font")
last = history.pop()
print("undo:", last)
print("undo:", history.pop())
print(history)
Output
undo: change font
undo: add image
['type title']
How It Works
Push on top. Each append() puts the newest edit at the end, which acts as the top of the stack.
Pop the top.history.pop() removes "change font", the most recent edit, and returns it.
Use it directly. The second pop happens inside print(), so "add image" is removed and shown in one line.
One edit left. Only the oldest action is still in the history.
Example 2 — Process a Printer Queue From the Front
pop(0) serves jobs in the order they arrived, until the list is empty.
python
jobs = ["report.pdf", "photo.png", "notes.txt"]
while jobs:
job = jobs.pop(0)
print("printing", job, "| waiting:", len(jobs))
try:
jobs.pop()
except IndexError:
print("nothing left to print")
Output
printing report.pdf | waiting: 2
printing photo.png | waiting: 1
printing notes.txt | waiting: 0
nothing left to print
How It Works
Front of the line.pop(0) removes the first job, so jobs print in arrival order: first in, first out.
Shrinking queue. After each pop, len() shows how many jobs are still waiting.
Empty list is falsy.while jobs: stops as soon as the last job is taken.
Popping nothing fails. One more pop() on the empty list raises IndexError, which is caught here.
Example 3 — Play Cards From the Middle of a Hand
Pop by a positive index and by a negative one, then try an index that does not exist.
python
hand = ["7H", "KS", "2D", "QC", "9S"]
print(hand.pop(1))
print(hand.pop(-2))
print(hand)
try:
hand.pop(10)
except IndexError:
print("index 10 is out of range")
print(len(hand))
Output
KS
QC
['7H', '2D', '9S']
index 10 is out of range
3
How It Works
Index 1.hand.pop(1) removes the second card, "KS", and the cards after it move left.
Counted on the new list. The hand is now 7H, 2D, QC, 9S, so pop(-2) removes "QC", the second card from the end.
Bad index raises. Only three cards remain, so index 10 raises IndexError instead of being clamped like insert().
Nothing lost. The failed pop removed nothing; the hand still holds three cards.
Frequently Asked Questions
The item it removed. pop() with no argument returns the last item, and pop(i) returns the item that was at index i.
pop() removes by position and returns the item. remove() removes the first item equal to a value and returns None.
Both remove the item at index i and raise IndexError for a bad index. pop() also returns the item; del returns nothing but can delete whole slices.
Check that the list is not empty first, for example with if lst: or while lst:, or wrap the call in try/except IndexError.
On a long list, yes: every remaining item shifts one place left. Use collections.deque and popleft() when you remove from the front a lot.