Technical skill practice
Python pop(): Remove and Return an Undo Item
Python list.pop removes an item and returns it. With no index it removes the last item, making it useful for a simple last-in, first-out stack. A supplied index selects a different position. Empty lists and invalid indexes raise IndexError.
Start with a small, explicit task
Keep three fictional editing actions in an undo history. Pop the latest action, remove a selected older action by index, and check the remaining history. Use separate small failure fixtures to inspect an empty stack and an out-of-range position without losing track of the successful operations.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_pop.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
add footer
['change color']
['IndexError', 'IndexError']
history = ["insert title", "change color", "add footer"]
latest = history.pop()
selected = history.pop(0)
assert latest == "add footer" and selected == "insert title"
assert history == ["change color"]
failures = []
for values, index in [([], -1), (["only"], 3)]:
try:
values.pop(index)
except IndexError:
failures.append("IndexError")
else:
raise AssertionError("expected invalid pop")
assert failures == ["IndexError", "IndexError"]
print(latest)
print(history)
print(failures)
Explain the result
The first pop returns add footer and shortens the list to two items. pop(0) then returns insert title from the beginning of that shortened list, leaving only change color. The returned value is the removed object, unlike append and remove, whose mutation is not returned as the changed list.
Both deliberate failures raise the same exception type for different reasons: one has no element at all, and the other requests a position beyond its one item. An actual undo interface may disable the action when its stack is empty. That is a user-interface choice layered on top of the list behavior.
Avoid these mistakes
Do not pass a value such as "change color" to list.pop expecting a value search; its argument is an index. A dictionary’s pop instead uses a key, so the shared method name does not make the interfaces interchangeable. For a long queue removed from the front, consider deque rather than treating this tiny example as a performance recommendation.
Questions, answered
How do I remove the last list item?
Use items.pop() and retain its return if you need the removed value.
Is pop the same as remove?
No. list.pop selects an index and returns the item; list.remove searches for a matching value.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.