from collections import deque stack = deque() # create an empty stack stack.append(1) # add elements to the top of the stack stack.append(2) stack.append(3) print(stack) # output: deque([1, 2, 3])
from collections import deque stack = deque([1, 2, 3]) # create a stack with some elements # remove elements from the top of the stack print(stack.pop()) # output: 3 print(stack.pop()) # output: 2 # check the current state of the stack print(stack) # output: deque([1])
from collections import deque stack = deque() # create an empty stack if not stack: # or "if len(stack) == 0:" print("The stack is empty") else: print("The stack is not empty") # output: The stack is emptyAs we can see, the "deque" implementation from the "collections" library is used in these examples.