Nested Dictionaries
Winter 2025
1
Adrian Salguero
Announcements
2
Recall: Dictionary Basics
3
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
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
Accessing Nested Dictionaries
6
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+"}
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
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?
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
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