Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction to nested lists
Iterating over nested lists
9. Calculating metrics for nested list columns
Modifying nested lists
Working with multiple lists
Summary

Instruction

Very good! After calculating the total donations on specific days, we want to calculate the total donations for specific locations. Things are going to get a bit more complicated because we are essentially calculating sums for "columns" instead of "rows". Take a look:

donations = [
  [345.0, 287.80, 119.27, 329.30],
  [294.25, 349.0, 178.90, 262.34],
  [401.0, 456.45, 289.43, 319.27]
]

place_sums = [0, 0, 0, 0]

for i, line in enumerate(donations):
  for j, value in enumerate(line):
    place_sums[j] += value

print(place_sums)

This time, we need to prepare a list with temporary values that will store column sums. Because we need an access to indexes, we used another kind of for loop – one that iterates over the indexes and values using the enumerate() function. The outer loop iterates over the rows of the list, while the inner loop iterates over values in each row. Inside the inner loop, we add the current value from the nested list (value) to the respective column sum (place_sums[j]).

Exercise

Create a list named averages that will store three elements:

  1. The average application score among all winners.
  2. The average application score among all runners-up.
  3. The average application score among all third-place finishers.

Stuck? Here's a hint!

First, create a nested loop in which you'll add all application scores to the respective elements in the averages variable.

Then, create a simple loop through the averages list and divide each element by 4.