Using Lists as a Stack : Stack : Collections PYTHON TUTORIALS


PYTHON TUTORIALS » Collections » Stack »

 

Using Lists as a Stack


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



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Collections
» Stack