codebear11
codebear11

Reputation: 19

Nested for loops with if/else in python

simple situation,

I have the following: When I just have an if statement, it works correctly.

products = [1,2,3,4,5]
orders = [1,2,3,4,5]

for product in products:
    for order in orders:
        if product == order:
            print("in list")

Results:

in list
in list
in list
in list
in list 

However when adding an else it doesn't work correctly.

for product in products:
    for order in orders:
        if product == order:
            print("in list")
        else:
            print("not in")

Results:

in list
not in
not in
not in
not in
not in

How can I fix it? Any help is appreciated.

Upvotes: 0

Views: 612

Answers (3)

alvas
alvas

Reputation: 122052

The set type is a better choice when you want to check if item is in a unique "list of item", e.g.

Lets consider the case where orders are non-unique and products are unique,

products = [1,2,3,4,5]
orders = set([1,2,3,4,5])

for p in products:
    if p in orders:
        print(f'product {p} in orders set')
    else:
        print(f'product {p} not in orders set')

Note: Here we use the f-string where you can print to see which p is in the order set.


Lets go back to the code in the original question, you have 2 lists and you want to iterate through the inner list for every outer list. Instead of calling it products/orders, lets call it outer/inner:

outer = [1,2,3,4,5]
inner = [6,7,8,9,0]

for o in outer:
    for i in inner:
        print(o, i)

[output]:

1 6
1 7
1 8
1 9
1 0
2 6
2 7
2 8
2 9
2 0
3 6
3 7
3 8
3 9
3 0
4 6
4 7
4 8
4 9
4 0
5 6
5 7
5 8
5 9
5 0

If you have a nested loop, for o in outer for i in inner, you will be iterating through all pairs of items in the outer list then the inner list.


Now lets go back to the if else part, if we are looping through the outer list then the inner list and if you only check on the outer list, with an if but not catching the else, you will not be seeing the results when it doesn't fall into the condition.

You can see this effect by adding the if-else after a print statement on every iteration, e.g.

outer = [1,2,3,4,5]
inner = [6,7,8,9,0]

for o in outer:
    for i in inner:
        print('printing always', o, i)
        if o + 5 == i: # We are checking if outer+5=inner, i.e. 1+5= 6, 2+5=7, etc. 
            print('printing only when outer+5=inner', o, i)

[out]:

printing always 1 6
printing only when outer+5=inner 1 6
printing always 1 7
printing always 1 8
printing always 1 9
printing always 1 0
printing always 2 6
printing always 2 7
printing only when outer+5=inner 2 7
printing always 2 8
printing always 2 9
printing always 2 0
printing always 3 6
printing always 3 7
printing always 3 8
printing only when outer+5=inner 3 8
printing always 3 9
printing always 3 0
printing always 4 6
printing always 4 7
printing always 4 8
printing always 4 9
printing only when outer+5=inner 4 9
printing always 4 0
printing always 5 6
printing always 5 7
printing always 5 8
printing always 5 9
printing always 5 0

Now, we see that the normal loop print always prints, and if condition prints only when the condition is met.

To check when what is printed with an if and else, you can do something like:

outer = [1,2,3,4,5]
inner = [6,7,8,9,0]

for o in outer:
    for i in inner:
        print('printing always', o, i)
        if o + 5 == i: # We are checking if outer+5=inner, i.e. 1+5= 6, 2+5=7, etc. 
            print('printing only when outer+5=inner', o, i)
        else:
            print('printing otherwise', o, i)
        print('------------')
            

[output]:

printing always 1 6
printing only when outer+5=inner 1 6
------------
printing always 1 7
printing otherwise 1 7
------------
printing always 1 8
printing otherwise 1 8
------------
printing always 1 9
printing otherwise 1 9
------------
printing always 1 0
printing otherwise 1 0
------------
printing always 2 6
printing otherwise 2 6
------------
printing always 2 7
printing only when outer+5=inner 2 7
------------
printing always 2 8
printing otherwise 2 8
------------
printing always 2 9
printing otherwise 2 9
------------
printing always 2 0
printing otherwise 2 0
------------
printing always 3 6
printing otherwise 3 6
------------
printing always 3 7
printing otherwise 3 7
------------
printing always 3 8
printing only when outer+5=inner 3 8
------------
printing always 3 9
printing otherwise 3 9
------------
printing always 3 0
printing otherwise 3 0
------------
printing always 4 6
printing otherwise 4 6
------------
printing always 4 7
printing otherwise 4 7
------------
printing always 4 8
printing otherwise 4 8
------------
printing always 4 9
printing only when outer+5=inner 4 9
------------
printing always 4 0
printing otherwise 4 0
------------
printing always 5 6
printing otherwise 5 6
------------
printing always 5 7
printing otherwise 5 7
------------
printing always 5 8
printing otherwise 5 8
------------
printing always 5 9
printing otherwise 5 9
------------
printing always 5 0
printing otherwise 5 0
------------

Upvotes: 3

1extralime
1extralime

Reputation: 626

This is working correctly. One the first loop its comparing 1 to 1,2,3,4,5 then 2 to 1,2,3,4,5. So because the if statement is true for at least one item in the loop, it will print every time its true. You don't have an else condition so every time it is not true, it doesn't do anything. The full output would be:

in list
not in
not in
not in
not in
not in
in list
not in
not in
not in
not in
not in
in list
not in
not in
not in
not in
not in
in list
not in
not in
not in
not in
not in
in list

Upvotes: 2

adshin21
adshin21

Reputation: 188

In the second code, the total print statement executed will be 25 ( len(products) * len(orders) ).

You'll still get 5 "in list" in output, because when if statement result false, it'll go to else statement and print "not in".

Upvotes: 1

Related Questions