Watch a Python list act as a LIFO stack — undo rewinds, brackets balance and a monotonic stack sweeps Next Greater Element.
The big picture
Meet the stack
A stack touches only its top. Whatever went in last comes out first — the rule behind undo, back buttons and every function call your interpreter makes.
1
LIFO rule
Push 12 then 7, and the first pop hands back 7. The newest value always leaves first.
2
A list is a stack
append() pushes onto the top, pop() removes the last item — both O(1). No new class needed.
3
Peek and underflow
stack[-1] reads the top without removing it. Popping an empty list raises IndexError — stack underflow, Python style.
4
Where you meet it
Undo history, browser back, matching brackets, the call stack and depth-first search all run on stacks.
5
The faster cousin
collections.deque gives O(1) append and pop on both ends — the hot-loop choice when a list would copy.
Meet the stack
LIFO rule1Push 12 then 7, and the first pop hands back 7. The newest value always leaves first.
A list is a stack2append() pushes onto the top, pop() removes the last item — both O(1). No new class needed.
Peek and underflow3stack[-1] reads the top without removing it. Popping an empty list raises IndexError — stack underflow, Python style.
Where you meet it4Undo history, browser back, matching brackets, the call stack and depth-first search all run on stacks.
The faster cousin5collections.deque gives O(1) append and pop on both ends — the hot-loop choice when a list would copy.
5 building blocks make up Meet the stack
Algorithm in motion
Undo rewind
Typing "hello", undoing twice, then finishing the word — watch the stack of snapshots rewind.
empty — top is -1
1 / 11
An empty stack of typed-text snapshots. Every keystroke pushes a new save point on top.
Visualize
NGE flow
The monotonic-stack loop for Next Greater Element: each index is pushed once and popped once — that is why it is O(n).
process decision input / output start / end
Watch it run
Walkthrough
A list doing honest stack duty — then the bracket checker that interviews love.
stack_ops.py
1stack =[]
2
3stack.append(12)
4stack.append(7)
5stack.append(21)
6print(stack[-1])
7
8stack.pop()
9stack.pop()
10print(stack)
main
stack starts as an empty list — Python’s simplest stack.
Memory
stack= []
Console
output appears as the program runs…
Step 1/8
Algorithm in motion
Monotonic sweep
Five values, one pass: the stack holds only indices still waiting for their next greater element.
4
0
i
8
1
5
2
2
3
9
4
1 / 12
Find the next greater element for every value. The stack keeps indices whose answer is still pending.
In 3D
Call stack in 3D
Every call pushes a frame, every return pops one — spin the pile while factorial unwinds.
Drag to orbit the scene. Press Play to watch memory in action.
Challenge
Predict the output
A sticky-key cleaner: "<" is a backspace that pops the last typed character.
predict.py
1stk =[]
2text ="he<lo<l"
3for ch in text:
4if ch =="<":
5 stk.pop()
6else:
7 stk.append(ch)
8print("".join(stk))
9print(len(stk))
Your predicted output — one line per console line
Hint: Walk the string one char at a time. Letters append, every "<" pops the newest character off the stack.
Practice
Concept check
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand
?append(x)drops into place
?pop()drops into place
?stack[-1]drops into place
?IndexErrordrops into place
?LIFOdrops into place
?dequedrops into place
?call stackdrops into place
?monotonic stackdrops into place
Recap
Key takeaways
Key takeaways6 takeaways
01
A Python list is a stack: append() to push, pop() to pop — both O(1).
02
stack[-1] peeks at the top; popping an empty list raises IndexError.
03
LIFO order: the last value pushed is the first value popped.
04
Undo history, browser back and the call stack are stacks you already use daily.
05
Balanced brackets need a stack: push openers, pop and match each closer.
06
The monotonic stack solves Next Greater Element in O(n) — push once, pop once per index.