Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMagnus Lindberg Christensen
2,231 PointsMultidimensional Lists
Hey
Why is it, that we can write
travel_expenses = [
[5.00, 2.75, 22.00, 0.00, 0.00],
[24.75, 5.50, 15.00, 22.00, 8.00],
[2.75, 5.50, 0.00, 29.00, 5.00],
]
print("Travel Expenses:")
week_number = 1
for week in travel_expenses:
print("* Week #{}: ${}".format(week_number, sum(week)))
week_number += 1
and the computer then knows what exact week it is, since a completely new variable was made?? I cannot see any relation between the two
2 Answers
Grigorij Schleifer
10,365 PointsHi Magnus, I hope I understand your question correctly.
The list travel_expenses contains three list items. That's why it is a multidimensional list. So when you use a for loop it will go over the first, second and third list inside travel_expenses. Since you created the variable week_number and assigned 1 to this variable, after every loop, you increment the week_number by one and print its value to the screen.
The relationship is: You have 3 lists that will take the for loop three times to go over it and you print and increment week_number accordingly. Plus every time you print the sum of the list that the for loop is looking at. One at a time.
Does this answers your question?
Magnus Lindberg Christensen
2,231 PointsPerfect, thank you!