Using Lists as a Stack : stack « Collections « Python Tutorial






stack = []

def pushit():
    stack.append("A")

def popit():
    if len(stack) == 0:
        print 'Cannot pop from an empty stack!'
    else:
        print 'Removed [', 'stack.pop()', ']'

def viewstack():
    print stack      # calls str() internally

pushit()
viewstack()
popit()
viewstack()








9.4.stack
9.4.1.Using Lists as a Stack