Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment for loop index when used with range in python?

Tags:

python

loops

I have a for loop as follows:

i=0 
while True:
    print("i initialized as", i)
    for i in range(0,3):
        print ("inside for",i)
        i = i + 1
    print("exit from for i value is",i)

Now inside the while infinite loop there is for and everytime inside for "i" iteration starts over from "0" ideally "i" should get the incremented value and start from 2, output is:

 inside for 0
 inside for 1
 inside for 2

 second iteration:

 inside for 0
 inside for 1
 inside for 2
 .
 .

Ideally output be like:

 inside for 0
 inside for 1
 inside for 2
 
 second iteration:
 inside for 3
 inside for 4
 inside for 5
 .
 .

So the point is "i" value is not saved and every time loop starts from 0, So how can i make "i" to start from the last incremented value before entering to for loop.

like image 921
golu kashyap Avatar asked Dec 17 '22 23:12

golu kashyap


2 Answers

You have no control over i when you're using for i in .... It's set to whatever the for loop decides when looping.

Here, just don't use i for your loop index, use an anonymous variable like _ to count 3 times, keep i as a "global" counter:

i=0
while i<10:  # just to stop after a while for the demo
 print("i initialized as", i)
 for _ in range(0,3):
      print ("inside for",i)
      i = i + 1
 print("exit from for i value is",i)

output:

inside for 0
inside for 1
inside for 2
exit from for i value is 3
i initialized as 3
inside for 3
inside for 4
inside for 5
exit from for i value is 6
i initialized as 6
inside for 6
inside for 7
inside for 8
exit from for i value is 9
i initialized as 9
inside for 9
inside for 10
inside for 11
exit from for i value is 12

Aside: while true should be while True. Unlike Java & C++, Booleans are capitalized in python.

like image 96
Jean-François Fabre Avatar answered Apr 19 '23 16:04

Jean-François Fabre


The problem here is that you are trying to modify the loop variable of a for loop. Python doesn't stop you from doing i = i + 1 inside the loop, but as soon as control goes back to the top of the loop i gets reset to whatever value is next in the sequence. That's just the way for loops work in Python, it also happens if you loop over a list or tuple rather than a range. Eg,

for i in (2, 3, 5, 7, 11):
    print(i)
    i = 10 * i
    print(i)

output

2
20
3
30
5
50
7
70
11
110

To get the output you want is easy, though. In the following code I replaced your infinite while loop with a small for loop.

i = 0 
for j in range(3):
    print("\ni initialized as", i)
    for i in range(i, i + 3):
        print ("inside for", i)
    i += 1

output

i initialized as 0
inside for 0
inside for 1
inside for 2

i initialized as 3
inside for 3
inside for 4
inside for 5

i initialized as 6
inside for 6
inside for 7
inside for 8

If the outer loop isn't infinite, you can do this sort of thing with a single loop and an if test. It's more compact, but slightly slower, since it has to perform the test on every loop iteration.

outer_loops = 4
inner_loops = 3
for i in range(outer_loops * inner_loops):
    if i % inner_loops == 0:
        print("\nIteration", i // inner_loops)
    print ("inside for", i)

output

Iteration 0
inside for 0
inside for 1
inside for 2

Iteration 1
inside for 3
inside for 4
inside for 5

Iteration 2
inside for 6
inside for 7
inside for 8

Iteration 3
inside for 9
inside for 10
inside for 11
like image 45
PM 2Ring Avatar answered Apr 19 '23 17:04

PM 2Ring