We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

Simple Stack

November 29, 1997 | Fredrik Lundh

The following Stack class allows you to store any kind of data in a first-in, last-out fashion. Note that items are appended to the end of the internal list object; this means that both push and pop are relatively fast operations.

Example: The Stack implementation (for Python 1.5.2 and later)
class Stack:

    def __init__(self):
        self.list = []

    def __len__(self):
        return len(self.list)

    def push(self, item):
        self.list.append(item)

    def pop(self):
        return self.list.pop() # default is last

In recent versions of Python, you can use collections.deque instead of a list object.