Open this example in the interactive library
Source code
from collections import deque
history = deque(maxlen=3)
history.extend([1, 2, 3, 4])
print(list(history))Sample output
[2, 3, 4]

Keep a bounded history. A self-contained example using the Python standard library.
Open this example in the interactive library
from collections import deque
history = deque(maxlen=3)
history.extend([1, 2, 3, 4])
print(list(history))[2, 3, 4]