Python list.pop(): Remove and Retrieve the Last Element
When you need to remove an item from the end of a list — and optionally use its value — .pop() is your tool. It does two things in one step: removes the last element from the list and returns it so you can work with it. This makes it especially powerful for implementing stacks, processing queues in reverse, or undoing the most recent action in an app.
How .pop() Works
stack = [10, 20, 30, 40]
# Removes and returns the last element:
removed = stack.pop()
print(removed) # 40
print(stack) # [10, 20, 30]
# Call it again:
removed = stack.pop()
print(removed) # 30
print(stack) # [10, 20]
Notice that .pop() modifies the original list. Each call shrinks the list by one.
Popping by Index
.pop() also accepts an optional index. Pass an index to remove (and return) any element, not just the last:
fruits = ["apple", "banana", "cherry", "date"]
# Remove the item at index 1:
removed = fruits.pop(1)
print(removed) # banana
print(fruits) # ['apple', 'cherry', 'date']
# No argument = last item:
fruits.pop()
print(fruits) # ['apple', 'cherry']
Real-World Example: Implementing an Undo Stack
One of the most classic uses of .pop() is building an undo system — every action gets pushed onto a stack, and undoing removes the most recent one:
actions = []
# User performs actions:
actions.append("typed 'Hello'")
actions.append("bold text")
actions.append("changed font size")
print("Current state:", actions)
# User hits Ctrl+Z (undo):
last_action = actions.pop()
print("Undid:", last_action) # Undid: changed font size
print("Current state:", actions) # ['typed \'Hello\'', 'bold text']
pop() vs. remove(): What's the Difference?
list.pop(i)— removes by index, returns the removed itemlist.remove(x)— removes by value (first match), returnsNone
nums = [5, 10, 15, 10, 20]
# remove() — finds value 10 and removes the first occurrence:
nums.remove(10)
print(nums) # [5, 15, 10, 20]
# pop() — removes by position:
nums.pop(0)
print(nums) # [15, 10, 20]
Common Mistakes to Avoid
- Calling .pop() on an empty list: Raises
IndexError. Always checkif my_list:first. - Ignoring the return value when you need it:
list.pop()returns the removed element — if you just writemy_list.pop()without capturing the result, the value is gone. - Popping with an out-of-range index:
my_list.pop(99)on a 3-item list raisesIndexError.
queue = []
# Safe pattern — check before popping:
if queue:
item = queue.pop()
else:
print("Nothing to remove — list is empty")
Key Takeaways
list.pop()removes and returns the last item;list.pop(i)removes and returns item at indexi.- It modifies the list in place — the removed item is gone from the list.
- Always guard against
IndexErrorby checking the list isn't empty first. - Use it for stacks (LIFO), undo systems, or any time you need to consume items from the end of a list.
What to Learn Next
With append() and pop() in your toolkit, you now have a fully functional stack data structure. Explore how stacks are used to solve real algorithm problems — like validating parentheses or reversing a string — and practice using len() to safely check list size before popping.
