Nested lists

A Python list can contain any value, so in fact, the items can themselves be lists. When a list is made up of other lists, we call it a nested list.

Here's a nested list representing the names and scores of three gymnasts:

gymnasts = [
             ["Brittany", 9.15, 9.4, 9.3, 9.2],
             ["Lea", 9, 8.8, 9.1, 9.5],
             ["Maya", 9.2, 8.7, 9.2, 8.8]
           ]

We can treat this like any other list, using bracket notation and list methods to interact with the list.

🧠 Check Your Understanding

What's the length of gymnasts?

What's the length of gymnasts[0]?

Accessing nested list items

Starting again with this nested list:

gymnasts = [
                ["Brittany", 9.15, 9.4, 9.3, 9.2],
                ["Lea", 9, 8.8, 9.1, 9.5],
                ["Maya", 9.2, 8.7, 9.2, 8.8]
            ]

To access each of the three lists in gymnasts, we use the same bracket notation from before:

gymnasts[0]     # ["Brittany", 9.15, 9.4, 9.3, 9.2]
gymnasts[1]     # ["Lea", 9, 8.8, 9.1, 9.5],
gymnasts[2]     # ["Maya", 9.2, 8.7, 9.2, 8.8]

We can also reach deep into the nested list and access items inside the inner lists, however. We just add another level of bracket notation!

gymnasts[0][0]  # "Brittany"
gymnasts[1][0]  # "Lea"
gymnasts[1][4]  # 9.5
gymnasts[1][5]  # 🚫 IndexError!
gymnasts[3][0]  # 🚫 IndexError!

🧠 Check Your Understanding

Using bracket notation, how would you access the final score of 8.8?

Modifying nested lists

We can replace the inner lists inside a nested list using bracket notation.

This code replaces the entire first list with a brand new list:

gymnasts[0] = ["Olivia", 8.75, 9.1, 9.6, 9.8]

We can also modify items inside nested lists, using either bracket notation or list methods.

gymnasts[1][0] = "Leah"
gymnasts[2][4] = 9.8
gymnasts[2].append(10.5)

None of this is new functionality; this is the same functionality you learnt from before. It all just works as a consequence of Python allowing lists to contain other lists. I like to explicitly point it out, however, since it's not always immediately obvious, and nested lists are such a powerful data structure in programming.

Looping through nested lists

To iterate through every item in a nested list, we can use a nested for loop: a for loop inside a for loop!

Starting from this nested list of just the numeric scores:

team_scores = [
                [9.15, 9.4, 9.3, 9.2],
                [9, 8.8, 9.1, 9.5],
                [9.2, 8.7, 9.2, 8.8]
              ]

We can sum them up using this nested for loop:

sum_scores = 0
for member_scores in team_scores:
    for score in member_scores:
        sum_scores += score

The trickiest thing with nested loops is keeping track of what each loop variable represents. In the above code, member_scores stores each of the inner lists in each iteration- first [9.15, 9.4, 9.3, 9.2], then [9, 8.8, 9.1, 9.5], finally [9.2, 8.7, 9.2, 8.8]. Then in the inner loop, score stores each of the numbers inside those lists, starting with 9.15 and ending with 8.8.

If you're ever not sure what your variable is storing, you can always print it out and double check.

➡️ Next up: Exercise: Nested lists