Miguel Carvalho
Miguel Carvalho

Reputation: 21

Printing a Python list over and over with the threading library

I want to write a program that prints all the elements in a list over and over again with 5 seconds of interval between each element. So if the list is [1,2,3] it would print first the 1, then wait 5 seconds, print the 2 wait 5 seconds, print the 3 wait 5 seconds,then go back to printing the 1 and waiting 5 seconds, etc...

This is the code I have to illustrate how I'm thinking but this doesn't work at all. I think I have to use the threading library because the program will be doing some repetitive tasks parallel to printing the elements in this list. '''

import threading

def func(list, i):
    print(list[i])
    if i < 4:
        i+=1
    else:
        i = 0
    threading.Timer(1,funcao, list, i).start()


list = ['1', '2', '3', '4']
func(list,0)

'''

How can I build such a program? (btw I'm new to programing so I'm sorry if this questions is basic but I coulnd't find any answers for this)

Upvotes: 1

Views: 296

Answers (3)

kederrac
kederrac

Reputation: 17322

you could use itertools.cycle to cycle your list and a while loop to forever continue the print process:

    from itertools import cycle
    import time

    def func(my_list):

        c = cycle(my_list)
        while True:
            print(next(c))
            time.sleep(5)

    func(['1', '2', '3', '4'])  

if you want to do other tasks in parallel you can use:

from itertools import cycle
from threading import Thread
import time

def func(my_list):

    c = cycle(my_list)
    while True:
        print(next(c))
        time.sleep(5)

t = Thread(target=func, args=(['1', '2', '3', '4'], ))
t.start()

# other code
# to test you may use:
# time.sleep(30)

Upvotes: 1

losifar luv
losifar luv

Reputation: 105

Try this.

import time

def func(lst):
    for x in lst:
        print(x, end=" ")
        time.sleep(5)
    func(lst)

func([1,2,3]) 

It's better to have stopping condition so you can modify it like that

import time

def func(lst,i):

    if i < 0:
        return

    for x in lst:
        print(x, end=" ")
        time.sleep(5)
    time.sleep(5)
    i -= 1
    print()
    func(lst,i)

func([1,2,3],2)

Upvotes: 0

d_s_m
d_s_m

Reputation: 67

I'm not 100% sure what you require with regards to the threading? But, if all you're after is a function that will take a list and repeatedly print the elements in the list one by one (indefinitely) whilst waiting a pre-defined number of seconds between each print, this function should do the trick.

You can use the sleep() function from the time module. The sleep() function suspends (waits) execution of the current thread for a given number of seconds.

import time

def func(my_list, seconds): 
        while True: 
            for item in my_list: 
                print(item)
                time.sleep(seconds)

Then you can just do something like:

my_list = [1,2,3]
seconds = 5 
func(my_list, seconds)

Upvotes: 0

Related Questions