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 trial

Python Introducing Lists Using Lists Multidimensional Lists

I do not understand the code at 5:48

I am trying to figure out what the code does and I dont understand how it knows what week to read first. I am confused so if someone could explain this code step by step that would really give me a lot of clarity.

Thanks in advance

2 Answers

Stivan Radev
Stivan Radev
1,475 Points
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)))

First of all to answer your question on how does it know which week to read.

If you type, len(travel_expenses), you will get 3. The reason why you will get 3 is because there are 3 lists inside of travel_expenses travel_expenses[0] would be [5.00, 2.75, 22.00, 0.00, 0.00], travel_expenses[1] would be [24.75, 5.50, 15.00, 22.00, 8.00], .... and on. for week in travel_expenses: ----- week is the new variable and it is going through all the stuff that are inside of travel_expenses, by all the stuff I mean the 3 lists that are inside it. So, when you first run the code, it runs through travel_expenses[0],which is [5.00, 2.75, 22.00, 0.00, 0.00], and it sums the total, then it runs through travel_expenses[1], which is [24.75, 5.50, 15.00, 22.00, 8.00], and it sums the total, and finally it does the last one travel_expenses[2], which is [2.75, 5.50, 0.00, 29.00, 5.00], and it sums the total.

Hope this helps a bit Good luck

Thank you very much

Maciej Doktorowicz
Maciej Doktorowicz
530 Points

Hi, thank you for the question and the answer! Related follow-up question: why don't we have to define week? Is the for...in loop special in a way, so that you don't have to define the variable that follows for?

I'd appreciate some help :)

Anders Stenborg Martin
Anders Stenborg Martin
1,474 Points

It's a default counter variable, which does not have to be defined in Python.