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 can use this: [None] * 10 . But this won't be "fixed size" you can still append, remove ... This is how lists are made. You could make it a tuple ( tuple([None] * 10) ) to fix its width, but again, you won't be able to change it (not in all cases, only if the items stored are mutable).
Lists by default are allowed to grow/shrink in Java. However, that does not mean you cannot have a List of a fixed size. You'll need to do some work and create a custom implementation. You can extend an ArrayList with custom implementations of the clear, add and remove methods.
A list has a variable size while a tuple has a fixed size.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With