44

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?

nunos
  • 20,479
  • 50
  • 119
  • 154
  • 4
    The python `for` loop is *not* the same thing as a the C `for` construct. Other languages would call it a `foreach` statement, if that is more familiar to you. – Martijn Pieters Apr 09 '13 at 13:13
  • 1
    i is a variable over a range. If you put i = 4 then you change i within the step of the current iteration. After the second iteration it goes on as expected with 3. If you wnat a different behaviour don't use range instead use a while loop. – SaCry Apr 09 '13 at 13:14
  • If you are using a for loop, you probably shouldn't change the index in multiple places like that. – kovac Feb 28 '18 at 10:49

6 Answers6

37

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.

Serdalis
  • 10,296
  • 2
  • 38
  • 58
18

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.

Pro Q
  • 4,391
  • 4
  • 43
  • 92
Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55
  • 4
    the range function does not create a list but a range object which is an iterable that is used by the for loop. This implies that you never have all the elements of the range in memory at the same time, they are generated on the fly. – Manuel Selva Nov 20 '19 at 15:05
  • @ManuelSelva Back in 2013 (the Python 2 days), `range()` did create a list. With Python 3, it no longer does. – 200_success Sep 12 '22 at 18:51
2

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.

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
1

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.

sth
  • 222,467
  • 53
  • 283
  • 367
1

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

0

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.

lvarayut
  • 13,963
  • 17
  • 63
  • 87