Practice Exercises: Python Dictionaries#

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.

# your code here

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

# your code here

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.

# your code here

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.

# your code here

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.

# your code here

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.

# your code here

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).

# your code here

8) Nested dictionary: counts by province AND mode#

Note: We haven’t done any examples with nested dictionaries, so this type of question would not show up on the test, but it is helpful to confirm your understanding of dictionaries.

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).

# your code here

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.

# your code here

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.

# your code here