We have seen how we can go through the elements of a list. What if we have a list made up of multiple lists? How can we loop through all of the individual elements?
Suppose we are in charge of a science class, that is split into three project teams:
project_teams = [["Ava", "Samantha", "James"], ["Lucille", "Zed"], ["Edgar", "Gabriel"]]
If we want to go through each student, we have to put one loop inside another:
for team in project_teams: for student in team: print(student)
This results in:
Ava Samantha James Lucille Zed Edgar Gabriel
Instructions
We have provided the list sales_data
that shows the numbers of different flavors of ice cream sold at three different locations of the fictional shop, Gilbert and Ilbert’s Scoop Shop. We want to sum up the total number of scoops sold. Start by defining a variable scoops_sold
and set it to zero.
Go through the sales_data
list. Call each inner list location
, and print out each location
list.
Within the sales_data
loop, go through each location
list and add the element to your scoops_sold
variable.
By the end, you should have the sum of every number in the sales_data
nested list.
Print out the value of scoops_sold
.