Dictionaries & Data — Lesson 3: Read a collection of records
New skill: Lists of dictionaries, fields and comparison
Previously learned: Units 01–08
Why this matters
This pattern appears in scoreboards, check-in logs, and reports built from data.
Step 1 — Explore working Python
Read the program below. Before running it, write down the result you expect.
records = [{'name': 'Kai', 'score': 11}, {'name': 'Lee', 'score': 15}]
best = records[0]
for record in records:
if record['score'] > best['score']:
best = record
print(best['name'])
Step 2 — Trace the code
- Read each record as a dictionary with
nameandscorekeys. - Begin with the first record as the current best.
- Visit each record and replace the best only for a strictly higher score.
Step 3 — Check your understanding
Which name wins if two records share the highest score and we compare using >?
Step 4 — Watch for this mistake
Starting the best score at 0 fails when every score is negative. Start from the first record.
Practice: Read a collection of records
Complete best_student(records) to return the name belonging to the highest score. In a tie, choose the first student. The input is nonempty.
Function contract: best_student(records)
Starter file: main.py
Work in the editor, using Run to explore and Check to compare against the published examples. Your submitted code, not the public check, determines the recorded score.
Hint 1 — Start
Start with the first record as your best.Hint 2 — Build
Compare the `score` fields, not the names.Hint 3 — Test
Use `>` rather than `>=` to preserve the first name in a tie.Reflect
In 3–5 sentences, explain the algorithm you chose, one test case you predicted, and how you would detect an incorrect answer.
Optional reading
For another perspective on this skill, see How to Think Like a Computer Scientist (interactive textbook). This Playground lesson, worked example, and exercise are original; no textbook passages were reproduced.