# FAQ: Learn Python: Loops - Nested Loops

I’m stuck on why and I don’t understand - one sums all of the figures, while one sums only those in the last list. But why that would be has not been explained or makes sense to me.

If you’re asking about the nesting it’s that Python groups blocks of code by indentation. So if I have a for loop every line I want to repeat should be indented. The next unindented line is not part of the loop at all, for example-

for _ in range(3): print("repeat") print("don't repeat\n\n")

If you want to repeat more than one line they must all be indented in order-

for _ in range(3): print("repeat") print("repeat this too")

Note that loops can be nested inside other loops. So in the given example we first look at a location, then we count how many scoops are made at this location using a loop but for a grand total this second loop must be repeated for each location. So we end up with a loop inside another loop as we want this second loop to repeat at each location.

If you want to track the details consider adding some `print` functions to your code so you can see at what point in the loop the code is currently executing. Maybe play around with the example below or your own code-

for i in range(1, 4): for letter in "abc": print(f"Outer loop iteration: {i}, letter = {letter}")

Sorry tgrtim, what I meant is that:

I don’t understand why the second part:

``````for a in location:
print(a)
``````

Only prints the elements in the last list of the three. I see that Python sees each list as its own element, and so a second loop is needed for Python to reach each element of each list. But I don’t understand why a separate print statement that is not a nested loop will only print out 5, 12, 13 and not all nine numbers.

The syntax of a `for` loop hides a couple of things. The first is that in each iteration you effectively assign a new reference to the target name, in this case `location` is being assigned to one of the nested lists from your `sales_data` such that `location = a_nested_list`.

As in your example when you print it out you can see a representation of the list object. But what that name refers to changes on each iteration of that loop. By the time you reach your second loop `for a in location: ...` location is only assigned to one list, the last list from `sales_data`.

You can try printing out `location` e.g. `print(location)` near your line 8 `print("")` if you wanted to see what it actually contains at that point.

1 Like

I have this same exact syntax but I keep getting an error through codeacademy

1 Like

Ref exercise 4 in the nested loops lesson.
I get the correct answer and my print statement is outside the nested loops so how come I get a “that does not look quite right…” comment and as a result I am unable to progress?

1 Like

I have just figured this out and the answer is dumb, I mean really stupid.
Did you like me delete the first print statement? If you did then reinstate it and you will get a nice blue cross!

1 Like

*** Edit***
I was able to fix the code because it required me to keep the print(location) from earlier in the exersize.

I keep getting the “The output does not look quite right. Is your final `print()` statment unindented?” but I feel like the code I have is not indented!

``````scoops_sold = 0
for location in sales_data:
for scoops in location:
scoops_sold += scoops
print(scoops_sold)
``````

I have moved them around and put spaces but I can’t seem to get it to pass me. I am getting 96 though.

1 Like
``````sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]

scoops_sold = 0

for location in sales_data:
#print(location)
#print(scoops_sold)
for scoop in location:
#print(scoop)
scoops_sold += scoop

print(scoops_sold)
``````

I’ve re-read and redone this a couple times now. The results add to a single printed “96”. Now-commented print statements for debugging. Even done this on my calculator. It keeps saying unexpected output. My print statement is outside the loop. Why is this not working?

Edit: Nevermind. It needs the location printed too.

For some reason this particular lesson was not straightforward.
If anybody is seeking guidance, this code did it for me:

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

Hey Jezza, did you figure it out? struggling here.
sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
scoops_sold = 0
for location in sales_data:
print(location)
for item in location:
scoops_sold += item
print(scoops_sold)

Question: How does the program know what is location?

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

scoops_sold = 0

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

print(scoops_sold)

I completely agree. I would say 95% of the lessons/modules are very good! I can almost always figure out where my mistakes are and/or where the lesson is headed. This one almost ended in frustration tears. I don’t like that.

The whole loops section leaves a lot to be desired. I don’t understand the whole indentation thing. Right now, it’s just guess and check. Looking at the solution just feels like cheating. lol

Here is my code, it seems to be correct, but I can’t progress.

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

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

``````# Loop through each sublist
for team in project_teams:
# Loop elements in each sublist
for student in team:
print(student)
``````

yeah this type of stuff, the fact that you can actually do this, is really cool.
made me smile when i saw it

This is how I had my code set up before I looked for the answer.

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

My code was wrong & after seeing the correct answer below, I’m just confused on where the “element” portion came in on the “for” “in” statement and then on the following line to set scoops_sold += element. This portion of the program had me struggling! After looking at the answer I would’ve never thought to do that . Any any explanations will greatly appreciated. Thank you!

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

When writing loops (nested or otherwise), you must look at the structure of the data you are processing. That structure will dictate how you write your loops. In the code snippet posted by you, `sales_data` is a list. The list has three elements with each element being a list itself. The elements of these sub-lists are numbers.

In your attempted solution, you have written a loop     `for location in sales_data:`
In each iteration of the loop, an element of the `sales_list` will be assigned to the loop variable `location`.

• In the first iteration, a reference to `[12, 17, 22]` will be assigned to `location`.
• In the second iteration, a reference to `[2, 10, 3]` will be assigned to `location`.
• In the third iteration, a reference to `[5, 12, 13]` will be assigned to `location`.

Then your second loop     `for scoops_sold in location:`    will begin. The last value that was assigned to the `location` variable happens to be `[5, 12, 13]`. So, your second loop will iterate over these elements.

• In the first iteration, `5` will be assigned to `scoops_sold`.
• In the second iteration, `12` will be assigned to `scoops_sold`.
• In the third iteration, `13` will be assigned to `scoops_sold`.
Then your second loop will terminate.

Contrast this with the solution code,

``````for location in sales_data:
print(location)
for element in location:
scoops_sold += element
print(scoops_sold)
``````

Because of the indentation, the second loop is nested within the body of the first loop.
In the first iteration of the outer loop, a reference to `[12, 17, 22]` will be assigned to `location`. The `location` will be printed. Then the second loop will commence and will iterate over the `[12, 17, 22]` list. The naming of the loop variable is your choice. You can pick any valid name which you deem suitable (but should try to pick names which contribute to better code readability as opposed to random or gibberish variable names). You can name your loop variable something other than `element` if so inclined.
The nested loop    `for element in location:`     will iterate over the `[12, 17, 22]` list.

• In the first iteration, `12` will be assigned to the loop variable `element`.
When dealing with numbers, the statement `scoops_sold += element` can be interpreted as being shorthand for `scoops_sold = scoops_sold + element`.
After the first iteration, `scoops_sold` will be `12`.

• In the second iteration of the nested loop, `17` will be assigned to `element`.
`scoops_sold` will be updated to `12 + 17` which is `29`.

• In the third iteration, `22` will be assigned to to element.
`scoops_sold` will be updated to `29 + 22` which is `51`.
Then the nested loop will terminate.

Then, the second iteration of the outer loop will commence and `[2, 10, 3]` will be assigned to `location`. I think you can walk through the rest of the process from hereon.

We are iterating over a list of sub-lists. The outer loop is meant to target the sub-lists. The inner loop is meant to iterate over the numbers in the sub-lists.

3 Likes

@mtrtmk thanks as always for your very detailed walkthrough and answer. I think this is very helpful for a lot of the students here who seemed to be confused about how the nested loops work.

1 Like