Python Introducing Lists Using Lists Multidimensional Lists

Ryan McGuire
Ryan McGuire
3,754 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

KRIS NIKOLAISEN
KRIS NIKOLAISEN
53,553 Points

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