stacks listing the pyliststack py module implementation of the stack adt using python list class stack creates an empty stack def __init__self )self _theitems list(returns true if the stack is empty or false otherwise def isemptyself )return lenself = returns the number of items in the stack def __len__ self )return lenself _theitems returns the top item on the stack without removing it def peekself )assert not self isempty()"cannot peek at an empty stackreturn self _theitems[- removes and returns the top item on the stack def popself )assert not self isempty()"cannot pop from an empty stackreturn self _theitems pop(push an item onto the top of the stack def pushselfitem )self _theitems appenditem the individual stack operations are easy to evaluate for the python list-based implementation isempty()len and peek(only require ( time the pop(and push(methods both require (ntime in the worst case since the underlying array used to implement the python list may have to be reallocated to accommodate the addition or removal of the top stack item when used in sequenceboth operations have an amortized cost of ( using linked list the python list-based implementation may not be the best choice for stacks with large number of push and pop operations remembereach append(and pop(list operation may require reallocation of the underlying array used to implement the list singly linked list can be used to implement the stack adtalleviating the concern over array reallocations to use linked listwe again must decide how to represent the stack structure with the python list implementation of the stackit was most efficient to use the end of the list as the top of the stack with linked listhoweverthe front of the list provides the most efficient representation for the top of the stack in we saw how to easily prepend nodes to the linked list as well as remove the first node the stack adt implemented using linked list is provided in listing