Home > Enterprise >  How to add the time.sleep() after every 100 iterations?
How to add the time.sleep() after every 100 iterations?

Time:06-13

For example, I have the next list:

`l = ['a1', 'c4', 'p8', ... , '9i']`

The list contains 1000 values -> print(len(l)) = 1000

My next action, it is iteration the list

for i in list:
    print(i)

I need to add time.sleep(10) to this process after every 100 iterations, but once time.sleep(60) should be after the first iteration (at the beginning). How I can to do this? Thanks

CodePudding user response:

Use enumerate to both iterate on the value and it's position

  • i==0 will sleep 60

  • i0==0 (100, 200, 300, ...) will sleep 10

for i, value in enumerate(values):
    print(value)
    if i == 0:
        time.sleep(60)
    elif i % 100 == 0:
        time.sleep(10)

CodePudding user response:

for i, item in enumerate(my_list, 1):
    if i == 1:
        time.sleep(60)
    elif i % 100:
        time.sleep(10)
    print(item)
  • Related