I would use collections.deque
with a maxlen
arg
>>> import collections
>>> d = collections.deque(maxlen=10)
>>> d
deque([], maxlen=10)
>>> for i in xrange(20):
... d.append(i)
...
>>> d
deque([10, 11, 12, 13, 14, 15, 16, 17, 18, 19], maxlen=10)
There is a recipe in the docs for deque
that is similar to what you want. My assertion that it’s the most efficient rests entirely on the fact that it’s implemented in C by an incredibly skilled crew that is in the habit of cranking out top notch code.