Changing step in Python loop


In Python 2.7 I want to modify the step of a for loop in function of the specifics conditions satisfied in the loop. Something like this:

step = 1
for i in range(1, 100, step):
    if ...... :
        step = 1
        #do stuff
        step = 2
        #do other stuff

but it seems that this can’t be done, step is always 1.


Asked By: David



You could add a skip check:

skip = 0
for i in range(1, 100):
    if skip != 0:
        skip -= 1
    if ...... :
        #do stuff
        skip = 1
        #do other stuff
Answered By: sainoba

You could do it with a while loop:

step = 1
i = 1
while i < 100:
    if ...... :
        step = 1
        #do stuff
        step = 2
        #do other stuff
Answered By: WholesomeGhost

you would need to increment step manually which can be done using a while loop. checkout difference between while and for loop.

The for statement iterates through a collection or iterable object or
generator function.

The while statement simply loops until a condition is False.

if you use a while loop your code would look something like this:

step = 1
i = 1
while i < 100:
    if ...... :
        step = 1
        #do stuff
        step = 2
        #do other stuff
    i = i + step
Answered By: OLIVER.KOO

If you want to over-complicate things, you could create a custom generator where you can use the generator.send method to pass in a new step during iteration.

def variable_step_generator(start, stop=None, step=1):
    if stop is None:
        start, stop = 0, start

    while start < stop:
        test_step = yield start

        if test_step is not None:
            step = test_step

        start += step

With usage like:

variable_step_range = variable_step_generator(1, 100)
for i in variable_step_range:
    print i
    if i == 10:
    if i == 90:

#  1,  2,  3,  4,  5,  6,  7,  8,  9, 
# 10, 20, 30, 40, 50, 60, 70, 80, 90, 
# 91, 92, 93, 94, 95, 96, 97, 98, 99

But this isn’t really much more than a wrapper around the while loop that the other answers suggest.

Answered By: Jared Goguen

The second line of your code creates a range object that is then used for the rest of the loop, and your code doesn’t modify that range object. And even if you did change the step, that wouldn’t change just the next element of the range, it would change the whole range object (that is, changing the step to 2 would make every element in the range 2 more than the previous). If you really want to, you can create a named object and modify it within the for loop, but that would be a rather messy thing to do.

You can also use another index separate from the main for loop one. For instance:

actual_index = 1
for for_loop_index in range(1,100):
   if condition1:
        actual_index = actual_index + 1
   if condition2:
        actual_index = actual_index + 2
   if actual_index > 99:

This would basically be a while loop, except with a hard limit on the number of iterations, which could be useful in some use cases.

Answered By: Acccumulation

np.arrange creates a (numpy) array with increasing values. The for-loop goes through all the values of the array. (Numpy is a powerful library for computations with numerical arrays)

import numpy as np
for i in np.arange(start,stop,stepwidth):
    # your stuff
Answered By: bieboebap
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.