Lesson 6.7: Nested Data Structures

Today’s Goals

  • Understand and use nested data (lists inside dicts, dicts inside lists, etc.)
  • Practice accessing and manipulating nested values
  • Apply nested data to real datasets

Warm-Up Question

  • Have you ever seen a dictionary inside a dictionary?
  • Where might that be useful?

Example: List of Dictionaries

students = [
    {"name": "Alice", "scores": [90, 85, 92]},
    {"name": "Bob", "scores": [70, 88, 81]}
]
print(students[0]["scores"][1])   # 85

Example: Dictionary of Dictionaries

grades = {
    "Alice": {"math": 90, "science": 85},
    "Bob": {"math": 70, "science": 88}
}
print(grades["Bob"]["science"])   # 88

Why Use Nested Data?

  • Real datasets are often hierarchical
  • Example: Movies → Genres → Ratings
  • Example: Schools → Students → Scores

Class Practice

  • Create a nested data structure for your chosen dataset
  • Try printing a specific value from it

Student Challenge

  • Write a program that loops through nested data to find:
    • A maximum value (e.g., highest score across subjects)
    • An average across categories

Wrap-Up

  • Nested data = powerful for representing real-world information
  • Next time: Review & rehearsal with debugging/data practice