from collections import deque # Create a deque with a maximum size of 5 history_log = deque(maxlen=5) # Add items to the log history_log.append('User A logged in') history_log.append('API call to /status') history_log.append('User B updated profile') print(f"Current log: {list(history_log)}") history_log.append('Database query failed') history_log.append('Admin C deleted item 123') print(f"Log after more additions: {list(history_log)}") # Adding one more item will automatically remove the oldest one history_log.append('Cron job started') print(f"Log after exceeding maxlen: {list(history_log)}") # Accessing elements (like a list, but more efficient for ends) print(f"Most recent item: {history_log[-1]}") print(f"Oldest item: {history_log[0]}") # Useful for maintaining a fixed-size buffer of recent events, actions, etc.