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
6. Iterating over values
Modifying nested lists
Working with multiple lists
Summary

Instruction

Great! We already know how to access specific elements in nested lists, but how can we iterate over all the elements? We need nested for loops. Here's an example:

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

for day in donations:
  for place in day:
    print(place)

First, we iterate over days in the donations variable. Because each day is itself a list, we can iterate over each place in each day and print it on a separate line. Pay attention to the indentation – the second for loop is further indented because it is part of the first loop.

Exercise

Create a set named hackathon_winners. Add all unique team names from the hackathon_results nested list to the set.

Stuck? Here's a hint!

Use hackathon_winners.add() inside a nested for loop.