# FAQ: Learn Python: Loops - Nested Loops

This community-built FAQ covers the “Nested Loops” exercise from the lesson “Learn Python: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Im struggling to finish this code:

``````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:
element += scoops_sold
print(scoops_sold)
``````

Im not sure how to make sure that each element gets added to the zero value variable. I dont know what Im doing wrong. Can someone show me and tell me what is going on here in this code?`Preformatted text`

Try switching those two variables around and see what happens.

1 Like

wow what a simple fix. Thank you so much!

1 Like

I’m not sure why the code below doesn’t work:

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

scoops_sold = 0
for sales in sales_data:
for scoops in sales:
scoops_sold += 1

it saying that scoops_sold is 0.

This loop iterates over the location list;

This loop iterates over the sales data;

The above should be adding the data to the result, not `1`. We are not incrementing the list, but accumulating it to arrive at a final total. Looking at the numbers it should be somewhere around 96.

Be sure your indentation is correct.

1 Like

Thank you for the explanation

1 Like

hey, so i have this code in:
sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
scoops_sold = 0

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

however when i try to run it it says:
TypeError: unsupported operand type(s) for +=: ‘int’ and ‘list’
idk how to change the the type or what to do

iterate over the inner list…

``for sale in location:``

How would I add only elements after a certain index value?
I have

``````sales_data = [['a',12, 17, 22], ['b',2, 10, 3], ['c',5, 12, 13]]

scoops_sold = 0

for location in sales_data[1:-1]:
print(location)
for element in location:
scoops_sold += element

print(scoops_sold)
``````

I want to add all values after the 0 index.
I tried

``````for location in sales_data[1:-1]:
``````

but the loop skipped over the set list [‘a’,12, 17, 22] and went to [‘b’,2, 10, 3]
Dennis

Did you try,

``````for location in sales_data:
print(location[0])
for element in location[1:]:
``````
1 Like

Many thanks! I did NOT try that variation.
Again, thank you!

``````sales_data = [['a',12, 17, 22], ['b',2, 10, 3], ['c',5, 12, 13]]
#add up only the second list
scoops_sold = 0

for location in sales_data[1:-1]:
print(location[0])
for element in location[1:]:
scoops_sold += element

print(scoops_sold)
``````

b
15

1 Like

I would remove the bolded part. We want to iterate the entire sales_data.

Duly noted! Thanks!
Here’s the new code:

``````sales_data = [['a',12, 17, 22], ['b',2, 10, 3], ['c',5, 12, 13]]

scoops_sold = 0

for location in sales_data:
#print(location[0])
for element in location[1:]:
scoops_sold += element

print(scoops_sold)
``````
1 Like

Hey I’m just wondering. I didn’t know how to complete the last part of this exercise because I think it’s the first time I see the “+=” operator. Maybe I’m wrong?

First time, but not the last. If we query `assignment operators` in search we find this to be among them.

It happens in two steps. The addition operation is performed where we take the current value and add the new value to it arriving at a new current value once it is assigned back to the variable.

``````new_current_value = old_current_value + new_value

a = a + b
``````

Both statements above express the same thing.

``````a += b
``````

expresses it in more succinct terms.

I was wondering why to use scoops_sold += element and not use scoops_sold.append(element)? I noticed .append does not work, but wonder why, as it should add a single element to a predefined list.

Welcome, @colinios,

If we examine the initial declarations, `scoops_sold` is set to zero. It is not a list, but a single numeric value which we will accumulate to reach total.

1 Like

result = 0
for count in range(4):
result = result + count

print(result)

output:

6 #Why???

0 + 1 + 2 + 3 == 6

Recall that the upper bound is exclusive.

1 Like