list.pop()
Remove and return an item by index — the last one by default. Mutates the list in place.
Common call
stack.pop()
Returns
the removed item; the list shrinks
Replaces
negative indexes count from the end (-1 = last)
Watch out
IndexError on an empty list or out-of-range index
list.pop(indexindex — Position of the item to remove. Negative counts from the end. Out of range raises IndexError.type: int · default: -1=-1)
→ Any
Demo
Live evaluation
Try:
Inputs
listlistcomma-separated items
indexint-1 = last
Output
['a', 'b', 'c'].pop()
'c'
The demo shows the RETURN value — the removed item. In real code the list also shrinks: after ["a","b","c"].pop() the list is ["a","b"]. Errors shown are exactly what Python raises.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| index | int | no (-1) | Position of the item to remove. Negative counts from the end. Out of range raises IndexError. |
Return value
Any — The removed item. The list itself shrinks by one — pop both mutates and returns.
Common patterns
Stack (LIFO)
append + pop from the end is the idiomatic Python stack — both O(1).
stack.append(job) job = stack.pop()
Consume a list backwards
Popping from the end avoids the O(n) shifting of pop(0).
while items: process(items.pop())
Safe pop with a default
list.pop has no default argument — guard the empty case yourself.
item = items.pop() if items else None
Examples
1. Pop the last item
["a", "b", "c"].pop()
Returns
'c'2. Pop by index
["a", "b", "c"].pop(0)
Returns
'a'3. Negative index
[1, 2, 3].pop(-2)
Returns
2Pitfalls
1. Popping from an empty list raises
Unlike dict.get there is no default — check first.
Raises
[].pop()
IndexError: pop from empty list
Guard
item = lst.pop() if lst else None
None (guarded)
2. pop(0) is O(n)
Every remaining element shifts left. Queues want collections.deque.
Slow queue
first = lst.pop(0)
O(n) shift on every call
Fix
from collections import deque q = deque(lst) first = q.popleft()
O(1)
3. pop both mutates and returns
Calling it just for the value still shrinks the list.
Item lost
last = lst.pop() # lst no longer contains last
list is shorter now
Peek without removing
last = lst[-1]
list unchanged
When to use
Use it
- Stack behavior — take back what you last appended
- Destructively consuming items one by one
- Remove by position AND need the removed value
Reach for something else
- Front-of-list queue → collections.deque.popleft
- Remove by value, not position → list.remove
- Just reading the last item → lst[-1]
Notes
Complexity
O(1) for the end; O(n) elsewhere (elements shift)
Return
the removed item; the list mutates
CPython impl
Objects/listobject.c :: list_pop_impl
Memory
May shrink the internal array when usage drops
Thread-safe
No — mutating shared lists needs a lock
FAQ
pop takes an index and returns the item; remove takes a value and returns None; del removes by index or slice and returns nothing.
lst.pop(0) # by index, returns item lst.remove("a") # by value del lst[0] # by index, no return
History
2.0
Core list method, unchanged semantics since.