Stacks and Queues
from collections import deque
q = deque() # empty
q = deque([1, 2, 3]) # with values
q.append(4) # append to right side
q.appendleft(0) # append to left side
print(q) # => deque([0, 1, 2, 3, 4])
x = q.pop() # remove & return from right
y = q.popleft() # remove & return from left
print(x) # => 4
print(y) # => 0
print(q) # => deque([1, 2, 3])
q.rotate(1) # rotate 1 step to the right
print(q) # => deque([3, 1, 2])
Deque is a double-ended queue with O(1) time for append/pop operations from both sides. Used as stacks and queues. See: Deque
Comments