3
3
3Create2021-06-10 01:52:54
Python
3Create, 2021-06-10 01:52:54

How to clear the list every interval of time?

Good night everybody!
I'm trying to understand how it's possible to clear the list by timer...
Numbers are written to the list every two seconds...
I want to delete (list.clear()) the contents of the list every 15 seconds.
I can't figure out how to do it. Help plz.

time_count=2

a=[]
b=0
while True:

    for i in range(time_count):
        sleep(1)
        
    b += 1
    a.append(b)

    print( "a:",a)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ramis, 2021-06-10
@3Create

I answered in the comments, please close the question

import time
my_list = []
data = 12
add_timer = int(time.time())
clear_timer = int(time.time())

while True:
    if int(time.time()) - clear_timer >= 15:
        clear_timer = int(time.time())
        my_list.clear() # Очищаем список каждые 15 сек
        print('Удаляем')
    if int(time.time()) - add_timer >= 2:
        add_timer = int(time.time())
        my_list.append(data) # Записываем числа в список каждые 2 сек
        print('Записываем')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question