How do I skip a few iterations in a for loop

Question:

In python I usually loop through ranges simply by

for i in range(100): 
    #do something

but now I want to skip a few steps in the loop. More specifically, I want something like continue(10) so that it would skip the whole loop and increase the counter by 10. If I were using a for loop in C I’d just sum 10 to i, but in Python that doesn’t really work.

Asked By: Alex S

||

Answers:

You cannot alter the target list (i in this case) of a for loop. Use a while loop instead:

while i < 10:
    i += 1
    if i == 2:
        i += 3

Alternatively, use an iterable and increment that:

from itertools import islice

numbers = iter(range(10))
for i in numbers:
    if i == 2:
        next(islice(numbers, 3, 3), None)  # consume 3

By assigning the result of iter() to a local variable, we can advance the loop sequence inside the loop using standard iteration tools (next(), or here, a shortened version of the itertools consume recipe). for normally calls iter() for us when looping over a iterator.

Answered By: Martijn Pieters

The best way is to assign the iterator a name – it is common have an iterable as opposed to an iterator (the difference being an iterable – for example a list – starts from the beginning each time you iterate over it). In this case, just use the iter() built-in function:

numbers = iter(range(100))

Then you can advance it inside the loop using the name. The best way to do this is with the itertools consume() recipe – as it is fast (it uses itertools functions to ensure the iteration happens in low-level code, making the process of consuming the values very fast, and avoids using up memory by storing the consumed values):

from itertools import islice
import collections

def consume(iterator, n):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
    else:
        # advance to the empty slice starting at position n
        next(islice(iterator, n, n), None)

By doing this, you can do something like:

numbers = iter(range(100))
for i in numbers: 
    ...
    if some_check(i):
        consume(numbers, 3)  # Skip 3 ahead.
Answered By: Gareth Latty

Why not just set the value to skip until? Like:

skip_until = 0
for i in range(100):
    if i < skip_until:
        continue
    if SOME_CONDITION:
        skip_until = i + 10
    DO_SOMETHING()

where SOME_CONDITION is whatever causes you to skip and DO_SOMETHING() is the actual loop contents?

Answered By: Josh Buell
for i in range(0, 100, 10):
    print(i)

will print 0, 10, 20 …

Answered By: Jonathan

A charmed and simplest form is like that:

>>> for i in range(5,10):
...     print (i)
... 
5
6
7
8
9

Where 5 was the index that started iteration.

Answered By: GunBoe

Something that I keep using. You can use below to get a 10% prob on running your code:

import random
for i in range(1000000):
    if random.random() > 0.1:
        continue:
    # your code
Answered By: RShravan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.