-1

I am trying to iterate over the list items in python based on some condition if met. I have 5 strings in the list and I want to consider the first element and check my condition. if only the condition meets, then I want to go to the second element and so on.

mylist = ["1","2","34","44","55",]
for m in mylist:
    pyperclip.copy(m)
    print(m) # I want to print it every 30 seconds after the condition meets!
    roll = "34"
    if roll == m:
        print(roll) # Now wait for next m from the list!

Explanation:

Let's say, I took 1 from the list, then i copied it and printed it. now before i print another m from the list, I have to wait for the condition. if roll is equal to m then print roll and wait until the next m has been printed (i.e. until those 30 seconds). I want to do it infinitely, Do I need while loop?

Please fell free to ask for more explanation.

Thanks!

2 Answers2

1

use while loop with % to keep going through the list infinite times

import time
mylist = ["1","2","34","44","55",]
i = 0
l = len(mylist)
while (True):
    m = mylist[i%l]
    # pyperclip.copy(m) # don't know what this is!?
    print(m) 
    roll = "34"
    if roll == m:
        print("printing roll: ",roll)
        time.sleep(30) # waiting 30 secs
    i+=1
1
2
34
printing roll:  34
44
55
1
2
34
printing roll:  34
44
55
1
2
34
printing roll:  34
44
55
1
2
34
........Infinite......
Kuldeep Singh Sidhu
  • 3,748
  • 2
  • 12
  • 22
0

you can refer below code, just add time.sleep(secs). I want to do it infinitely if looping over a list, loops till the end of the list

import time
mylist = ["1","2","34","44","55",]
for m in mylist:
    pyperclip.copy(m)
    print(m) # I want to print it every 30 seconds after the condition meets!
    time.sleep(30)
    roll = "34"
    if roll == m:
        print(roll) # Now wait for next m from the list!
  • That is only a pseudo code, which i think is not described well in my question. Let me tell you again. if the list items end, i want to start it again. i want to match each list item to the roll value, and if equal, then print roll. if not equal wait for next m from the list, which is 30 seconds! – Aayush Khawaja Apr 27 '20 at 05:55
  • what happens if list[0] is matched to roll? – lone_ranger Apr 27 '20 at 05:58
  • then the roll value will be printed. and it should wait for 30 seconds to look for the list[1] value and so on. let me give you an example. – Aayush Khawaja Apr 27 '20 at 06:01
  • You have 100 strings in the list like "1", "2"... Now, you get some random values each second like "2","23"....Now you want to compare these values to the first list items. if value and list item is equal the do something. otherwise wait for 30 seconds and check the second item from the list to these values and so on. if the list ends, start it again. because the values are random and they can change. – Aayush Khawaja Apr 27 '20 at 06:06