1 of 11

Nested Dictionaries

Winter 2025

1

Adrian Salguero

2 of 11

Announcements

  • Homework 4, Part 1 due Monday February 10th by 11:59pm
  • Midterm next Wednesday, February 12th during class
    • Monday’s Lecture will be a Midterm review, come with questions!
    • Scored low on midterm? → a better grade on final will replace midterm grade
  • Coding Practice 5 now available
    • Will be released on Ed if website issues continue to persist

2

3 of 11

Recall: Dictionary Basics

  • How do we declare a dictionary?
  • How do we add to a dictionary?
  • How do we modify a dictionary?
  • How do we check if a key exists in a dictionary?
  • How do we get the keys, values, or pair-entries of a dictionary?

3

4 of 11

Review: Dictionary Practice

Write a Python function that takes in a list of grades and returns a dictionary where the keys are the letter grade and the value is the count of how many times that grade appears in the list.

grades = ["A", "A", "B", "B", "B", "C", "F"]

d = {"A": 2, "B": 3, "C": 1, "F": 1}

Hint: Think about what you should do when you encounter a new letter grade for the first time? Is it different from when encountering that letter a second time? Or third time?

4

5 of 11

Recall: Nested Loops

list = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]

How can we access the element 6 inside of this nested list?

5

We use double indices, e.g. list[1][2]

list[1] → [4, 5, 6]

list[1][2] → 6

6 of 11

Accessing Nested Dictionaries

  • Lists use double indices, but dictionaries do not have indices!
  • Instead you use double keys
    • First key tells us which dictionary to look at
    • Second key tells us which value of that selected dictionary to look at
  • You can expand that to dictionaries of dictionaries of dictionaries…

6

7 of 11

Example of a Nested Dictionary

7

students = {"Will, Smith": {"ID": 1234567,

"email": "wsmith@ucsd.edu",

"gender": "M", "grade": "B+"},

"Jose, Garcia": {"ID": 1231234,

"email": "jgarcia@ucsd.edu",

"gender": "M", "grade": "A"},

"Maria, Vera": {"ID": 1112234,

"email": "mvera@ucsd.edu",

"gender": "F", "grade": "A+"}

8 of 11

How can I access Maria’s email address?

8

students = {"Will, Smith": {"ID": 1234567,

"email": "wsmith@ucsd.edu",

"gender": "M", "grade": "B+"},

"Jose, Garcia": {"ID": 1231234,

"email": "jgarcia@ucsd.edu",

"gender": "M", "grade": "A"},

"Maria, Vera": {"ID": 1112234,

"email": "mvera@ucsd.edu",

"gender": "F", "grade": "A+"}

9 of 11

9

students = {"Will, Smith": {"ID": 1234567,

"email": "wsmith@ucsd.edu",

"gender": "M", "grade": "B+"},

"Jose, Garcia": {"ID": 1231234,

"email": "jgarcia@ucsd.edu",

"gender": "M", "grade": "A"},

"Maria, Vera": {"ID": 1112234,

"email": "mvera@ucsd.edu",

"gender": "F", "grade": "A+"}

# How can I determine if Jose, Garcia is in this class?

# How do I access Maria’s email?

# How do I update Will’s grade?

# How do I create a list of every student’s grade?

10 of 11

Nested Dictionary Practice Problem

regions = {"North America": {"USA": {"GDP": 27.36, "Population": 335},

"Canada": {"GDP": 2.14, "Population": 40}},

"Latin America": {"Mexico": {"GDP": 1.79, "Population": 128},

"Guatemala": {"GDP": 0.10, "Population": 6},

"El Salvador": {"GDP": 0.03, "Population": 6},

"Honduras": {"GDP": 0.03, "Population": 10}},

"Northern Europe": {"UK": {"GDP": 3, "Population": 68},

"Norway": {"GDP": 0.49, "Population": 5},

"Sweden": {"GDP": 0.60, "Population": 10}}}

Write a program that prints out each region’s average GDP (in trillions) and population size (in millions)

North America average GDP: ___

North America average population: ___

Latin America average GDP: ___

10

11 of 11

Nested Dictionary Solution

for region in regions.keys():

region_gdp_total = 0

region_population_total = 0

country_count = 0

for country in regions[region].keys():

region_gdp_total+=regions[region][country]["GDP"]

region_population_total+=regions[region][country]["Population"]

country_count+=1

print(region, "average GDP:", region_gdp_total/country_count)

print(region, "average population", region_population_total/country_count)

11