from collections import deque # As a queue (FIFO - First-In, First-Out) queue = deque() queue.append("task1") queue.append("task2") print(f"Queue after appending: {list(queue)}") first_task = queue.popleft() # Efficiently remove from the left print(f"Removed: {first_task}, Queue remaining: {list(queue)}") # As a stack (LIFO - Last-In, First-Out) stack = deque() stack.append("itemA") stack.append("itemB") print(f"Stack after appending: {list(stack)}") last_item = stack.pop() # Efficiently remove from the right print(f"Removed: {last_item}, Stack remaining: {list(stack)}") # Limiting size (e.g., for recent history) history = deque(maxlen=3) history.append("page1") history.append("page2") history.append("page3") print(f"History (full): {list(history)}") history.append("page4") # 'page1' is automatically removed print(f"History (after new append): {list(history)}")