Mini Project: Gradebook
Lesson 6 of 6 · View course roadmap
Learn the idea
Real-world data nests: lists of dicts, dicts of lists. A JSON API response is exactly this shape. Let's build a gradebook — a list where each student is a dict:
students = [{"name": "Ada", "grades": [90, 95]}, ...]
Patterns you'll use constantly:
- Loop the outer list, reach into each dict:
for s in students: print(s["name"]) - Compute per-item stats:
sum(s["grades"]) / len(s["grades"]) - Find the best item by tracking a "best so far" while looping
Where you'll use this
This gradebook shape — a list of dicts — is byte-for-byte what you get from requests.get(...).json(), a CSV DictReader, or a database ORM. Master the drill-down and every API is readable.
Common mistakes
- Guessing the structure instead of checking: print one element (
print(data[0])) before writing access code. - Deep chains with no safety:
d["a"]["b"]["c"]crashes on the first missing level — use.get()with defaults for optional branches. - Tracking 'best so far' but initialising best_avg to 0 — fails when all averages are negative. Initialise to the first element or -infinity.
Pro tip
max(students, key=lambda s: sum(s['grades'])/len(s['grades'])) finds the top student in one line — the key= pattern replaces the whole tracking loop.
Try it yourself
The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.
Output appears here…
Pass the challenge +45 XP
Using the starter data, print each student's name and average (1 decimal, format Name: avg), then print the name of the student with the highest average.
Maya: 90.0 Leo: 94.3 Zoe: 75.0 Leo
Track best_name and best_avg while looping; f"{avg:.1f}" formats to 1 decimal.
students = [
{"name": "Maya", "grades": [80, 90, 100]},
{"name": "Leo", "grades": [95, 92, 96]},
{"name": "Zoe", "grades": [70, 75, 80]},
]
best_name = ""
best_avg = -1
for s in students:
avg = sum(s["grades"]) / len(s["grades"])
print(f"{s['name']}: {avg:.1f}")
if avg > best_avg:
best_avg = avg
best_name = s["name"]
print(best_name)
Run your code to check it…
Check your understanding
Your notes (saved on this device)
Tip: use ← and → to move between lessons, ⌘K to search everything.