Alex
Alex

Reputation: 167

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

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

Upvotes: 1

Views: 1231

Answers (3)

user19355475
user19355475

Reputation:

this should work:

import time

first_iteration = True

for x in list:
  print(x)
  if first_iteration:
    time.sleep(60)
  elif x % 100 == 0:
    time.sleep(10)
  first_iteration = False

Upvotes: 0

Tenessy
Tenessy

Reputation: 9

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

Upvotes: 0

azro
azro

Reputation: 54168

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

  • i==0 will sleep 60

  • i%100==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)

Upvotes: 2

Related Questions