How to force a list to a fixed size?

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 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

Tags:

Python

List