Changing iteration variable inside for loop in Python

Question:

I am trying to do something as simple as changing the varible in which I am iterating over (i) but I am getting different behaviours in both Python and C.

In Python,

for i in range(10):
    print i,
    if i == 2:
        i = 4;

I get 0 1 2 3 4 5 6 7 8 9, but the equivalent in C:

int i;
for (i = 0; i < 10; i++) {
    printf("%d", i);
    if (i == 2)
        i = 4;
}

I get 01256789 (note that numbers 3 and 4 don’t appear, as expected).

What’s happening here?

Asked By: nunos

||

Answers:

Python has a few nice things happening in the background of for loops.
For example:

for i in range(10):

will constantly set i to be the next element in the range 0-10 no matter what.

If you want to do the equivalent in python you would do:

i = 0
while i < 10:
    print(i)
    if i == 2:
        i = 4
    else:      # these line are
        i += 1 # the correct way
    i += 1 # note that this is wrong if you want 1,2,4,5,6,7,8,9

If you are trying to convert it to C then you have to remember that the i++ in the for loop will always add to the i.

Answered By: Serdalis

Python gives you the elements in range(10), one after another. C repeatedly increments a variable.

Both of them don’t really care what else you do with the variable inside the loop, but since the two language constructs do slightly different things, the outcome is different in some cases.

Answered By: sth

When you call range(10) you create an iteratable list [0,1,2,3,4,5,6,7,8,9].

And the for loop just pick up one number after the other from the list at each turn, whether or not you haved changed the value of i.

Answered By: Stephane Rolland

The function range() creates a list.
For example, range(10) will create the following list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].

When you say for i in range(10), first off all the list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] will be generated, and then i will be assigned all the values from that list, in order.

It doesn’t matter if you change the value of i because on the next iteration it will be assigned the next element from the list.

It C/C++ on the other hand, at the end of each iteration the value of i is incremented and then compared to the maximum allowed value (in this case 9) and if it is greater, then the loop ends.

Answered By: Ionut Hulub

It’s because when you use the range() function in python. Your variable i will be go through the value in range. For example,

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

However, the C language that you have written is just using the normally condition to change the value of i. There is no function involved.

Answered By: lvarayut

You can not do this by using range function.
you have to do it by using while loop only because for loop uses range function and in range function variable will get incremented by its internal method no matter what you specify in the loop it will get incremented by range list only.
for i in range(10):
… print i
… if i == 2:
… i = 4
… else:
… i += 1

0
1
2
3
4
5
6
7
8
9

An interesting example is here….

for i in range(10):
… print i
… i = i + 10
… print i

this will print…
0
10
1
11
2
12
3
13
4
14
5
15
6
16
7
17
8
18
9
19

Answered By: Pradeep Kanegave
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.