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

Ryan McGuire
Ryan McGuire
3,758 Points

How would we iterate across the days if we wanted to sum up the expenses from all Mondays?

Using the For loop, it recognizes that week will refer to each row. How can we cycle through the columns, so that I could discern if Mondays are my most expensive day, because I'm tired, take an uber and eat a lot.

1 Answer

You could add another loop to sum values for the days then take the max:

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],
]

week_number = 1
day_number = 0

days = [0,0,0,0,0]


for week in travel_expenses:
  print("* Week #{}: ${}".format(week_number, sum(week)))
  week_number += 1

  day_number = 0

  for day in week: 
    days[day_number] += day
    day_number +=1 

print(days)
print("Max expense of " + str(max(days)) + " on day " + str(days.index(max(days)))) 

Note: the days go from 0 = Monday to 4 = Friday