In Python, We can use list as Stack, which is default . no need to import extra library or packages and list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”).
To add an item to the top of the stack, use append().
To retrieve an item from the top of the stack, use pop() without an explicit index.
>>> stack = [3, 4, 5] >>> stack.append(6) # add at the end or top of stack >>> stack.append(7) >>> stack [3, 4, 5, 6, 7] >>> stack.pop() # retrieve top element from list or stack 7 >>> stack [3, 4, 5, 6] >>> stack.pop() 6 >>> stack.pop() 5 >>> stack [3, 4]