Skip to the content

Nested data

Python · Unit 7: Dictionaries

A list can hold dictionaries: one per person, each with their own data.

First you pick the dictionary by its index and then the data by its key: team[0]["pts"].

team = [
    {"name": "Amy", "pts": 7},
    {"name": "Leo", "pts": 9},
]
print(team[0]["pts"])
print(len(team))

Prints

7
2

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Find the bug

    This program should print the points of Amy, the first on the team. Which line has the bug?

  3. 3. Complete the code

    Complete it to print each player's name.

  4. 4. Predict the output

    What does this code print?

  5. 5. Predict the output

    What does this code print?

  6. 6. Multiple choice

    Amy got a 10 on another test. Which line adds it without wiping out her other grades?

  7. 7. Complete the code

    Complete it to print "Zoe".

  8. 8. Put the lines in order

    Put in order the function that returns one person's average. grades holds a list of grades per name.

  9. 9. Find the case that fails

    best(team) should return the name of whoever has the most points, or "" if the team is empty. Which call does it fail with?

  10. 10. Predict the output

    Everyone wrote down their spending for the week. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →