Nested Loops Confusion

Im having trouble with loops, for some reason i just cant wrap my head around it. Maybe its the random variables that come in when and where to put them. Mainly having trouble with nested loops.

sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
scoops_sold = 0
for location in sales_data:
print(location)
for t in location:
scoops_sold += t
print(scoops_sold)

for location in sales_data:
print(location)
first loop takes an integer in the list and prints into a grid formation then repeats untill all numbers are out

for t in location:
scoops_sold += t
print(scoops_sold)
the second loop takes a number from the first loop and adds it to scoops_sold which = 0

so if the first loop takes the first number from the list then the second loop takes that same number and adds it to scoops_sold then repeats this and continues to add to the total value of scoops_sold?

how does the code know to go thru each number of the list, increasing index postion without a range() function?

i assume it knows its the end of the list because we are using a for loop. but is the loop just processing all numbers simutamiously and at the same time adding them all together into one large sum to = scoops_sold?

i think i answered a couple of my own questions just by writing it all out lol… but let me know if anyone has any good videos or deeper breakdown explantions of loops (mainly nested ones) thanks in advanced!

# Your code:

sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
scoops_sold = 0

for location in sales_data:
    print(location)
    for t in location:
        scoops_sold += t
        
print(scoops_sold)

You have a list sales_data.
sales_data has 3 elements and every element also happens to be a list itself.
The loop variables can be given any syntactically valid name, but it is better to choose names which contribute to better readability as opposed to random names.

for location in sales_data:

This loop iterates over the sales_data list and in every iteration, an element of sales_data is assigned to the loop variable location.

for t in location:

Since every element of sales_data is a list, so location will be a list. We can iterate over each of these lists and add to the running total of scoops_sold.

Basically, this is what is happening:

  • First iteration of outer loop:      location is [12, 17, 22]

    • First iteration of inner loop:       t is 12 and scoops_sold will be 0 + 12 which evaluates to 12.
    • Second iteration of inner loop:       t is 17 and scoops_sold will be 12 + 17 which evaluates to 29.
    • Third iteration of inner loop:       t is 22 and scoops_sold will be 29 + 22 which evaluates to 51.

  • Second iteration of outer loop:      location is [2, 10, 3]

    • First iteration of inner loop:       t is 2 and scoops_sold will be 51 + 2 which evaluates to 53.
    • Second iteration of inner loop:       t is 10 and scoops_sold will be 53 + 10 which evaluates to 63.
    • Third iteration of inner loop:       t is 3 and scoops_sold will be 63 + 3 which evaluates to 66.

  • Third iteration of outer loop:      location is [5, 12, 13]

    • First iteration of inner loop:       t is 5 and scoops_sold will be 66 + 5 which evaluates to 71.
    • Second iteration of inner loop:       t is 12 and scoops_sold will be 71 + 12 which evaluates to 83.
    • Third iteration of inner loop:       t is 13 and scoops_sold will be 83 + 13 which evaluates to 96.

If you wanted to do the same by iterating over the indexes, then you could do something like:

sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
scoops_sold = 0

for i in range(len(sales_data)):
    print(sales_data[i])
    for j in range(len(sales_data[i])):
        scoops_sold += sales_data[i][j]
        
print(scoops_sold)

Since we are not looking to change the elements of sales_data and only want to do some calculations with the values, then clearly the first version is much simpler as we don’t have to bother with keeping track of the index. If we did need to do some re-assignment, then we can opt for the second version.

2 Likes

thank you so much for the great break down. Helped me alot!

1 Like