Add entry to beginning of list and remove the last one
Use collections.deque:
>>> import collections
>>> q = collections.deque(["herp", "derp", "blah", "what", "da.."])
>>> q.appendleft('wuggah')
>>> q.pop()
'da..'
>>> q
deque(['wuggah', 'herp', 'derp', 'blah', 'what'])
Use collections.deque
In [21]: from collections import deque
In [22]: d = deque([], 3)
In [24]: for c in '12345678':
....: d.appendleft(c)
....: print d
....:
deque(['1'], maxlen=3)
deque(['2', '1'], maxlen=3)
deque(['3', '2', '1'], maxlen=3)
deque(['4', '3', '2'], maxlen=3)
deque(['5', '4', '3'], maxlen=3)
deque(['6', '5', '4'], maxlen=3)
deque(['7', '6', '5'], maxlen=3)
deque(['8', '7', '6'], maxlen=3)
Use insert()
to place an item at the beginning of the list:
myList.insert(0, "wuggah")
Use pop()
to remove and return an item in the list. Pop with no arguments pops the last item in the list
myList.pop() #removes and returns "da..."