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):

    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.


A Django site. rendered by a django application. hosted by webfaction.