8
votes

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
    else:
        step = 2
        #do other stuff

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

Thanks.

6
You can re-write as a while and increment step manually... - Jon Clements♦

6 Answers

15
votes

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
    else:
        step = 2
        #do other stuff
    i = i + step
6
votes
import numpy as np
for i in np.arange(start,stop,stepwidth):
    # your stuff
2
votes

You could do it with a while loop:

step = 1
i = 1
while i < 100:
    if ...... :
        step = 1
        #do stuff
    else:
        step = 2
        #do other stuff
    i+=step
2
votes

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
            yield

        start += step

With usage like:

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

#  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.

0
votes

You could add a skip check:

skip = 0
for i in range(1, 100):
    if skip != 0:
        skip -= 1
        continue
    if ...... :
        #do stuff
    else:
        skip = 1
        #do other stuff
0
votes

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:
        break

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.