PCC·06 · Chapter record
Dictionaries
Chapter 6 — storing and connecting related information
Five sessions on Python's other core data structure: building a dictionary, looping through it three different ways, checking membership, and nesting lists and dictionaries inside each other. Each one explains the concept, lists the key terms, then gives you exercises — try them yourself before revealing the code.
A Simple Dictionary
Concept
- A dictionary is a collection of key-value pairs, wrapped in braces
{}— each key connects to a value, separated by a colon. - Access a value with
dict[key]; add or modify one withdict[key] = value— a dictionary can start empty ({}) and grow one pair at a time. del dict[key]removes a key-value pair permanently.- Asking for a key that doesn't exist raises a KeyError —
.get(key, default)is the safe alternative, returning your default instead of crashing.
Key Terms
- dictionary
- a collection of key-value pairs, written with { }
- key-value pair
- a key connected to its value by a colon
- KeyError
- raised when you ask for a key that doesn't exist
- .get(key, default)
- safely reads a value, returning default if the key is missing
Exercise
Store a person's first name, last name, age, and city in a dictionary, then print each piece of information.
Build a small glossary: use five programming terms as keys and their meanings as values, then print each term and meaning.
Looping Through a Dictionary
Concept
for key, value in d.items():hands you both the key and the value together, one pair per iteration.- Loop over
d.keys()(or justfor x in d:— looping a dictionary directly defaults to its keys) when you only need the keys. - Loop over
d.values()when you only need the values — wrap it inset()to drop duplicates. - Wrap
sorted()around.keys()to loop in alphabetical order instead of insertion order.
Key Terms
- .items()
- loop target yielding (key, value) pairs
- .keys() / .values()
- loop targets yielding just the keys, or just the values
- set()
- a collection of unique items — handy for de-duplicating values
Exercise
Make a dictionary of three rivers and the country each runs through, then use a loop to print a sentence about each one.
Using that same dictionary, loop through just the river names, then separately loop through just the countries.
Checking Membership
Concept
- Use
inor not in with.keys()to check whether something is already in a dictionary —'erin' not in favorite_languages.keys(). - Combine this with a separate list to give some entries special treatment while looping — like printing a bonus message only for names that are also in a "friends" list.
- This scales to a dictionary with a million entries exactly as easily as one with five.
Key Terms
- in / not in (with .keys())
- checks whether a key exists in a dictionary
Exercise
Make a list of people who should take a poll, some already in your dictionary and some not. Loop through it, thanking those who've responded and inviting those who haven't.
Nesting: Lists & Dictionaries
Concept
- A list of dictionaries lets you manage many similar objects — each alien is its own
{}inside one[]list. - A list inside a dictionary lets one key hold multiple values —
'toppings': ['mushrooms', 'extra cheese']. - Nest a
forloop inside aforloop to walk through each dictionary entry's list value in turn.
Key Terms
- list of dictionaries
- a [] whose elements are each a {}
- list value
- a dictionary value that is itself a [] of items
Exercise
Make three small dictionaries representing pets (each with a kind and an owner), store them in a list, then loop through and print a sentence about each pet.
Store a pizza order with a crust and a list of toppings. Print a summary sentence, then loop through the toppings list to print each one.
A Dictionary in a Dictionary
Concept
- A dictionary can hold another dictionary as a value — ideal for many users, each with their own set of details.
- Loop the outer dictionary with
.items(); for each username you get an inner dictionary you can index or loop again. - Keep nesting shallow — if you find yourself several levels deep, there's usually a simpler structure.
Key Terms
- dictionary of dictionaries
- a {} whose values are themselves {}
Exercise
Build a dictionary of at least two usernames, each mapping to an inner dictionary of first name, last name, and location. Loop through it and print a formatted profile for each user.