简体   繁体   中英

How can I each one element of the list in the infinity loop

I have an infinity loop, where I send some request, and I wanna send an element of list from the first to the last and over and over again. Example:

my_list = ['a', 'b', 'c']
while True:
    myfunc(my_list)
    '''
    first iterate: 'a',
    second iterate: 'b', third iterate: 'c',
    fourth iterate: 'a', and so on.
    '''

You can use itertools.cycle :

from itertools import cycle

my_list = ['a', 'b', 'c']
for element in cycle(my_list):
    print(element)

# output: a b c a b c a b c ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM