使用deque,指定maxlen参数的值为N,例如: >>> from collections import deque >>> dq = deque(maxlen=3) >>> dq.append(1) >>> dq.append(2) >>> dq.append(3) >>> dq.append(4) >>> dq.append(5) >>> print(dq) deque([3, 4, 5], maxlen=3) 参考 Python Cookbook 1.3