Kickstart 2020 with new opportunities! - hours only!Up to 80% off on all courses and bundles.-Close
Introduction to nested lists
Iterating over nested lists
7. Calculating metrics for entire nested lists
Modifying nested lists
Working with multiple lists
Summary

Instruction

Very well done! Naturally, we can do more than just print each element. In the following example, we'll calculate the average value from all values in the nested lists:

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

total_sum = 0
total_count = 0

for day in donations:
  for place in day:
    total_sum += place
    total_count += 1

average = total_sum/total_count

Before iterating over the nested lists, we created two temporary variables: total_sum and total_count. Inside the nested loops, we updated their values accordingly. After the iteration process, we divided the first value (total_sum) by the second value (total_count) to get the average.

Exercise

We are now given more detailed hackathon results – instead of just team names, hackathon_results now stores tuples with team names and their application scores, which range from 0 to 100.

Calculate how many times each team scored more than 75 points. Create a count_good_results dictionary. The keys should be team names, and the values should be the number of times a given team got more than 75 points.

Stuck? Here's a hint!

Inside the nested loop, unpack the given tuple:

for application in hackathon_results:
  for team in application:
    name, score = team