Loading section...
Stack Fundamentals: The LIFO Discipline
Concepts: pyStackFundamentals, pyLIFO
A stack is a pile. You put things on top, you take things from the top. That is LIFO: last in, first out. The book you just placed on the pile is the first one you can grab. In Python, a plain list works perfectly as a stack. append() pushes to the top in O(1). pop() removes from the top in O(1). There is no stack class in Python's standard library because list already nails it. When the Stack Is the Right Tool list vs collections.deque for Stacks In an interview, say: 'I will use a Python list as a stack. append() is O(1) amortized and pop() from the right is O(1). If I needed O(1) guaranteed without amortization, I would use collections.deque.' That one sentence shows you know the tradeoff without overthinking it.