Skip to content

Data structures and algorithms · Stacks and queues

Stacks and queues

The order you take things out in is the whole design. · 8 minutes

A stack is last in, first out — a pile of plates. In Python it is just a list: append to push and pop() to take the top. A queue is first in, first out — a line at a shop. collections.deque gives one: append joins the back, popleft() serves the front.

Predict first

What does this print?

1stack = []
2stack.append("a")
3stack.append("b")
4stack.append("c")
5stack.pop()
6print(stack.pop())