How to force a list to a fixed size?

In Python 3, I want to create a list that will contain the last 5 variables entered into it.

Here is an example:

>>>l = []
>>>l.append('apple')
>>>l.append('orange')
>>>l.append('grape')
>>>l.append('banana')
>>>l.append('mango')
>>>print(l)
['apple','orange','grape','banana','mango']
>>>l.append('kiwi')
>>>print(l)
['orange','grape','banana','mango','kiwi'] #only 5 items in list

So, in python, is there any way to achieve what is demonstrated above? The variable does not need to be a list, I just used it as an example.


You might want to use a collections.deque object with the maxlen constructor argument instead:

>>>l = collections.deque(maxlen=5)
>>>l.append('apple')
>>>l.append('orange')
>>>l.append('grape')
>>>l.append('banana')
>>>l.append('mango')
>>>print(l)
deque(['apple','orange','grape','banana','mango'], maxlen=5)
>>>l.append('kiwi')
>>>print(l)
deque(['orange','grape','banana','mango','kiwi'], maxlen=5) #only 5 items in list

I ran into this same issue... maxlen=5 from deque was NOT a supported option due to access speed / reliability issues.

SIMPLE Solution:

l = []
l.append(x)                         # add 'x' to right side of list
l = l[-5:]                          # maxlen=5

After you append, just redefine 'l' as the most recent five elements of 'l'.

print(l)

Call it Done.

For your purposes you could stop right there... but I needed a popleft(). Whereas pop() removes an item from the right where it was just appended... pop(0) removes it from the left:

if len(l) == 5:                     # if the length of list 'l' has reached 5 
    right_in_left_out = l.pop(0)    # l.popleft()
else:                               #
    right_in_left_out = None        # return 'None' if not fully populated

Hat tip to James at Tradewave.net

No need for class functions or deque.

Further... to append left and pop right:

l = []
l.insert(0, x)                      # l.appendleft(x)
l = l[-5:]                          # maxlen=5

Would be your appendleft() equivalent should you want to front load your list without using deque

Finally, if you choose to append from the left...

if len(l) == 5:                     # if the length of list 'l' has reached 5 
    left_in_right_out = l.pop()     # pop() from right side
else:                               #
    left_in_right_out = None        # return 'None' if not fully populated

You could subclass list

>>> class L(list):
...     def append(self, item):
...         list.append(self, item)
...         if len(self) > 5: del self[0]
... 
>>> l = L()
>>> l.append('apple')
>>> l.append('orange')
>>> l.append('grape')
>>> l.append('banana')
>>> l.append('mango')
>>> print(l)
['apple', 'orange', 'grape', 'banana', 'mango']
>>> l.append('kiwi')
>>> print(l)
['orange', 'grape', 'banana', 'mango', 'kiwi']
>>>