collections.deque

A deque (say "deck") is a double-ended queue. Adding or removing at either end is O(1): append, appendleft, pop and popleft.

Remember that list.pop(0) is O(n). So whenever you need a queue, first in first out, use a deque. Breadth-first search in interviews almost always starts with queue = deque([start]) and loops on queue.popleft().

deque(maxlen=3) keeps only the newest 3 items. Appending a fourth drops the oldest one from the other end. That's a one-line "last N events" buffer or sliding window.

The trade-off: indexing into the middle, like queue[500], is O(n). A deque is for the ends.

Run it, then use appendleft on recent and see which end drops.