Algorithmsstack basics

Stack Basics — LIFO for Students

TT
Testlaa Team
May 14, 20261 min read

A stack is last-in-first-out (LIFO): you add (push) and remove (pop) from the same end—the top.

Try this in Python

stack: list[int] = []
stack.append(1)
stack.append(2)
stack.append(3)
print(stack.pop(), stack.pop(), stack.pop())

Key takeaways

  • Think of one open end only.
  • In Python, list.append + list.pop() is the usual student stack.
  • Never pop(0) if you care about speed—that is a queue pattern, not a stack.

Tags:

Stacks & queuesPythonStudents