0

Let's say I want to create a list. The list need to have a MAX length of 5. The list would operate as such:

list = []
list.append(1)
list = [1]
list.append(2)
list = [1,2]
..
list.append(5)
list = [1,2,3,4,5]

But, when I append another number the first element is removed:

list.append(6)
list = [2,3,4,5,6]

This is super basic and I can't figure this one out.

I don't want to use classes - can this be done with basic functions such as slices?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    You *are already using classes*. **everything** in Python is an object, **everything** is an instance of a class. – juanpa.arrivillaga Jan 25 '22 at 19:38
  • 2
    You can build your own function to append items that checks the length of the list and pops the first element if necessary. [Or you could just use a `collections.deque` and be done with it](https://stackoverflow.com/a/1931603/2648811) – Green Cloak Guy Jan 25 '22 at 19:38
  • 1
    `deque` (double ended queue) supports a max length: https://docs.python.org/3/library/collections.html#collections.deque – doctorlove Jan 25 '22 at 19:39

3 Answers3

3

You could use a collections.deque for this:

>>> import collections
>>> data = collections.deque(maxlen=5)
>>> data.append(1)
>>> data.append(2)
>>> data.append(3)
>>> data.append(4)
>>> data.append(5)
>>> data
deque([1, 2, 3, 4, 5], maxlen=5)
>>> data.append(6)
>>> data
deque([2, 3, 4, 5, 6], maxlen=5)
>>>

Note, this can pretty much work like a list object, however, it does have different performance characteristics, perhaps most importantly, no random access (basically, a linked list of arrays, but it won't have constant-time access like an array-list, which is what a list object is).

However, it is particularly meant for these types of operations, a "double-ended queue", it allows for efficient removal and addition at both ends. A list can only efficiently append and pop, but adding / removing from the beginning will be inefficient.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
3

If you need it to be a list, you can use a slightly different approach to add the items (in-place):

L = []
L[:] = L[-4:] + [1] # [1]
L[:] = L[-4:] + [2] # [1, 2]
L[:] = L[-4:] + [3] # [1, 2, 3]
L[:] = L[-4:] + [4] # [1, 2, 3, 4]
L[:] = L[-4:] + [5] # [1, 2, 3, 4, 5]
L[:] = L[-4:] + [6] # [2, 3, 4, 5, 6]

Which you could place in a function to make it easier to use:

def appendMax(L,maxLen,value):            # maxLen > 1
    L[:] = L[1-maxLen:] + [value]

L = []
appendMax(L,5,1) # [1]
appendMax(L,5,2) # [1, 2]
appendMax(L,5,3) # [1, 2, 3]
appendMax(L,5,4) # [1, 2, 3, 4]
appendMax(L,5,5) # [1, 2, 3, 4, 5]
appendMax(L,5,6) # [2, 3, 4, 5, 6]
Alain T.
  • 40,517
  • 4
  • 31
  • 51
2

Use deque() from collections. It will hold certain amount of variables, after that it will delete the first item while appending.

If you don't want to use libraries or classes, you can simply create a list with N none values inside it, then loop inside the list to replace the values inside it.

Olca Orakcı
  • 372
  • 3
  • 12