0

I am attempting to create an array in python that is capped at 60 values. My array starts with 0 values and then every minute, it adds a value to the array. When a new value is added, I want the oldest value added, i.e the last to get removed when the newest value is added (keeping the length at 60). Here is (parts) of my code so far...

pList = []
i = 0
while(True):
  pcList.append(x)
  if(i >= 60):
    #??
  i = i + 1
  time.sleep(60)

I am unsure about how to go about removing the last (first value) when it gets to 60 values when adding a new one.

hpaulj
  • 221,503
  • 14
  • 230
  • 353

1 Answers1

0

As mentioned in the comments, there are other, better ways to accomplish this, but, answering your specific question, and correcting your code for a couple of issues, the following illustrates how to add and remove elements from a list, based on length of the list.

Note that I shortened your time to make it easier to watch the process take place.

Also, I replaced i's value as your test to the length of the list itself as the test, as your question specifies.

There's a typo at pcList.append(x) and that's corrected below to `pList.

import time

pList = []
i = 0
while(True):
  pList.append(i)
  if len(pList) >= 60:
    pList.pop(0)
  i = i + 1
  print(pList)
  time.sleep(.25)
Chris Larson
  • 1,684
  • 1
  • 11
  • 19