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