How to constantly update a list of fixed length in python?

前端 未结 1 1183
时光取名叫无心
时光取名叫无心 2021-01-25 00:09

Sorry for the noob question. I\'m just starting to code and I need to keep track of the 1hr price history.

I wanted pull values every second into a list of size 3600 un

1条回答
  •  悲&欢浪女
    2021-01-25 00:27

    You could append each new entry, then check the length of the list and conditionally priceHistory.pop(0).

    Looks like you're initializing the priceHistory list in the loop though. You want to do that before the loop, so you don't end up assigning empty lists to priceHistory every time.

    Alternatively, and this depends on the use case, is that you can create an initial list with 3600 None entries, then just append and pop every time without the conditional length check.

    0 讨论(0)
提交回复
热议问题