Stacks & Play History
In short: a stack can only be touched at the top — push adds an
item, pop removes the top item, and peek looks without removing.
That one restriction gives LIFO: last in, first out. A Python list is
already a stack as long as you only ever use its end — append/pop() are
O(1), because the end is the free slot. It's the structure behind undo,
matching brackets, evaluating expressions and even how function calls work.

Press back on Tunebox's play history
stack = []
for track in ["A", "B", "C"]:
stack.append(track)
stack.pop()
stack.append("D")
print(stack[-1], len(stack))The mistake: pressing back with no history. Popping an empty list doesn't
return None — it raises:
history = ["Intro"]
history.pop()
print(history.pop())❓ Cross-question — "Why the end of the list and not the front?" Because the end is free —
append/pop()touch only the last slot: O(1).insert(0, x)/pop(0)shift every other item: O(n), the same cost from the linked-list lesson.