Practice Exercises: Python Dictionaries — Questions & Solutions#
These exercises were generated by ChatGPT using our course notebooks as input. They were briefly reviewed to make sure that they are correct and fit within the topics and level of the course, but they aren’t necessarily a comprehensive set of examples around the course topics.
1) Build a dictionary from two parallel lists#
Given:
districts = ["Scarborough", "North York", "Etobicoke"]
populations = [632098, 869401, 365143]
Create a dictionary pop_by_district mapping each district name to its population.
Print the dictionary.
districts = ["Scarborough", "North York", "Etobicoke"]
populations = [632098, 869401, 365143]
pop_by_district = {}
for i in range(len(districts)):
# use the index to connect the two lists
pop_by_district[districts[i]] = populations[i]
pop_by_district
2) Handling a case when a key is missing#
Given:
pop_by_district = {"Scarborough": 632098, "North York": 869401, "Etobicoke": 365143}
queries = ["York", "North York", "Scarborough", "Downtown"]
Create a list results containing the population for each query.
If a district is not in the dictionary, use the string "unknown" instead.
Tip: remember the in operator
pop_by_district = {
"Scarborough": 632098,
"North York": 869401,
"Etobicoke": 365143
}
queries = ["York", "North York", "Scarborough", "Downtown"]
results = []
for q in queries:
if q in pop_by_district:
results.append(pop_by_district[q])
else:
results.append("unknown")
results
3) Count categories (frequency dictionary)#
Given survey responses:
modes = ["car", "Car", "transit", "bike", "CAR", "walk", "transit", "car "]
Create a dictionary mode_counts that counts how many times each mode appears.
Treat different capitalization and extra spaces as the same category.
modes = ["car", "Car", "transit", "bike", "CAR", "walk", "transit", "car "]
mode_counts = {}
for m in modes:
key = m.strip().lower()
if key in mode_counts:
mode_counts[key] += 1
else:
mode_counts[key] = 1
mode_counts
4) Rename variables using a dictionary (column-rename style)#
This mirrors the idea of renaming census columns.
Given:
old_names = ["HH_ID", "AGEGRP", "EMPIN", "HDGREE", "PR"]
newnames = {
"HH_ID": "Household ID",
"AGEGRP": "Age group",
"EMPIN": "Employment income",
"HDGREE": "Highest education",
"PR": "Province"
}
Create a new list new_names where each code in old_names is replaced by its full name.
If a code is missing from newnames, keep the original code.
old_names = ["HH_ID", "AGEGRP", "EMPIN", "HDGREE", "PR"]
newnames = {
"HH_ID": "Household ID",
"AGEGRP": "Age group",
"EMPIN": "Employment income",
"HDGREE": "Highest education",
"PR": "Province"
}
new_names = []
for name in old_names:
if name in newnames:
new_names.append(newnames[name])
else:
new_names.append(name)
new_names
5) Invert a dictionary (swap keys and values)#
Given:
province_codes = {"ON": "Ontario", "QC": "Quebec", "BC": "British Columbia"}
Create a new dictionary code_by_province mapping province name → code.
Print the result.
province_codes = {
"ON": "Ontario",
"QC": "Quebec",
"BC": "British Columbia"
}
code_by_province = {}
for code in province_codes:
province = province_codes[code]
code_by_province[province] = code
code_by_province
6) Sum values by group (aggregation)#
You are given a list of (province, income) pairs:
income_rows = [
("ON", 52000),
("ON", 61000),
("QC", 48000),
("BC", 55000),
("QC", 51000)
]
Create a dictionary total_income_by_province mapping province code → total income.
Then print the dictionary.
income_rows = [
("ON", 52000),
("ON", 61000),
("QC", 48000),
("BC", 55000),
("QC", 51000)
]
totals = {}
for province, income in income_rows:
if province in totals:
totals[province] += income
else:
totals[province] = income
totals
7) Compute average by group using two dictionaries#
Using the same income_rows list from Q6, compute the average income per province.
Hint: maintain two dictionaries:
one for totals
one for counts
Create a dictionary avg_income_by_province with province code → average income (float).
counts = {}
totals = {}
for province, income in income_rows:
if province in totals:
totals[province] += income
counts[province] += 1
else:
totals[province] = income
counts[province] = 1
averages = {}
for province in totals:
averages[province] = totals[province] / counts[province]
averages
8) Nested dictionary: counts by province AND mode#
Given:
trips = [
{"province": "ON", "mode": "car"},
{"province": "ON", "mode": "transit"},
{"province": "QC", "mode": "car"},
{"province": "ON", "mode": "car"},
{"province": "BC", "mode": "bike"},
{"province": "QC", "mode": "transit"}
]
Create a nested dictionary counts so that you can look up counts like:
counts["ON"]["car"]counts["QC"]["transit"]
Missing combinations should simply not exist (no need to pre-fill zeros).
trips = [
{"province": "ON", "mode": "car"},
{"province": "ON", "mode": "transit"},
{"province": "QC", "mode": "car"},
{"province": "ON", "mode": "car"},
{"province": "BC", "mode": "bike"},
{"province": "QC", "mode": "transit"}
]
counts = {}
for trip in trips:
province = trip["province"]
mode = trip["mode"]
if province not in counts:
counts[province] = {}
if mode in counts[province]:
counts[province][mode] += 1
else:
counts[province][mode] = 1
counts
9) Find the key with the largest value#
Given:
mode_counts = {"car": 12, "transit": 9, "walk": 3, "bike": 5}
Write a loop to find:
top_mode(the key with the largest value)top_count(the largest value) Print both.
mode_counts = {
"car": 12,
"transit": 9,
"walk": 3,
"bike": 5
}
top_mode = None
top_count = -1
for mode in mode_counts:
if mode_counts[mode] > top_count:
top_mode = mode
top_count = mode_counts[mode]
top_mode, top_count
10) Parse simple “key=value” strings into a dictionary#
Given:
lines = [
"province=ON",
"agegrp=25-34",
"income=61000",
"education=Bachelor"
]
Create a dictionary record with keys and values from the lines.
Then convert record["income"] to an int.
Print the final dictionary.
lines = [
"province=ON",
"agegrp=25-34",
"income=61000",
"education=Bachelor"
]
record = {}
for line in lines:
key, value = line.split("=", 1)
record[key] = value
if "income" in record and record["income"].isdigit():
record["income"] = int(record["income"])
record