Home > Mobile >  Getting different results while executing similar "for loops"
Getting different results while executing similar "for loops"

Time:01-11

Code 1

>>> L=[0,1,2,3]
    for i in range(len(L)):
        print(f"Counter {i}")
        for j in range(len(L)):
            print(j)
            if len(L)==4:
                L.remove(2)
                L.remove(3)
            else:
                pass

[Output] Counter 0
         0
         1
         2
         3
         Counter 1
         0
         1
         Counter 2
         0
         1
         Counter 3
         0
         1

Code 2

>>> L=[0,1,2,3]
    for i in L:
        print(f"Counter {i}")
        for j in L:
            print(j)
            if len(L)==4:
                L.remove(2)
                L.remove(3)
            else:
                pass

[Output] Counter 0
         0
         1
         Counter 1
         0
         1

The two codes are similar but they are giving different results.

In the first code the length of L is 4, so the variable i in first for loop will take values 0,1,2 and 3. For i=0, j again can take 4 values. But in the second loop we make the length of list to 2. So this effect will reflect, when i=1 and so on as can be seen from the output of the code.

But in the second code, after deleting two elements of the list in the second loop, its effect becomes transparent in the next iteration of the second loop.

Why this is so? I am not able to understand whether we use for i in range(len(L)) or for i in L, its effect should be same in the output of 2 codes. In the first code, after deleting two elements of list range(L) does not change immediately, while in the second code we get different outputs.

Can someone explain why this is so?

CodePudding user response:

It's because for the first one, you iterate over a range, meaning you've calculated the number of iterations from the start (4). In the second one, you iterate over a list, meaning it will keep on looping until it runs out of items in the list. Because you remove two items from the list during iteration, it will run out of items two loops sooner.

In the first piece of code, the range(len(L)) is recalculated for each iteration of the outer loop, but that doesn't mean that the outer loop's range(len(L)) will be reevaluated as well.

CodePudding user response:

L=[0,1,2,3] for i in range(len(L)) -> for i in range(4) for i in L -> for i in [0,1,2,3]

point is while you are using range(len(L)) and than trying to remove from the list it will not affect for loop as it is not dependent on List elements (i.e It is not pointing to the list). only the first time while using range(len(L))->4 now if you remove from the list or even empty the list this will not affect.

but in for i in L you are directly pointing to the list if any thing changes it will affect the for loop also.

see the below image

enter image description here

Image 1 as you can see it is iterating independent of the list.

enter image description here

Image 2 .Dependent on the list

  • Related