-2

For this code:

for i, num in enumerate(range(20)):
    if num > 10:
        i += 1
    print(num)

The idea is to get it to skip every other iteration after num > 10, however it just prints all the items in the range.

Is there a way to skip the next/several iteration(s) of a for loop?

burritab
  • 25
  • 4

1 Answers1

0

skip every other iteration after num > 10

Generate a range that fulfils that without needing anything special within the loop itself:

from itertools import chain

for i, num in enumerate(chain(range(10), range(10, 20, 2))):
    print(i, num)
deceze
  • 510,633
  • 85
  • 743
  • 889