Dictionaries
📖 Dictionaries
🔑 Creating and Accessing Dictionaries
person["email"] raises a KeyError immediately if the key doesn't exist — there's no silent None the way some languages return. Use .get() whenever a key might legitimately be missing; it returns None (or your own default value) instead of crashing.
✏️ Modifying Dictionaries
🔁 Iterating Over Dictionaries
Plain for name in scores: (no .keys()) also works and iterates over keys — .keys() is optional but makes the intent explicit. .items() is the one to reach for whenever you need both the key and value together, pairing naturally with the tuple unpacking from the previous chapter.
Key-Value Collections: Go vs Kotlin vs Python
| Language | Type | Missing key behavior |
|---|---|---|
| Go | map[string]int | Returns the zero value silently (e.g. 0) — no crash, no error, which can hide real bugs. The comma-ok idiom (v, ok := m[key]) is needed to actually detect a missing key. |
| Kotlin | Map<K, V> / MutableMap<K, V> | map[key] returns a nullable type (V?); map.getValue(key) throws if missing, directly comparable to Python's bracket-indexing/.get() split. |
| Python | dict | d[key] raises KeyError immediately; d.get(key, default) returns a default instead. |
⚡ Dict Comprehensions
The same comprehension pattern from the last chapter works for dictionaries too, using {key: value for ...}:
Creating & Accessing
{key: value} literal, d[key] indexing, d.get(key, default) for safety.
Modifying
Assign to add/overwrite, del d[key] or d.pop(key) to remove.
Iterating
.keys(), .values(), .items() — .items() for key+value pairs together.
Dict comprehension
{k: v for k, v in ... if condition} — same pattern as list comprehensions.
💻 Coding Challenges
Challenge 1: Word Frequency Counter
Given the list words = ["apple", "banana", "apple", "cherry", "banana", "apple"], build a dictionary counting how many times each word appears, using a loop and .get() with a default.
Goal: Practice the common "build a count dictionary from a list" pattern.
Challenge 2: Invert a Dictionary
Given capitals = {"France": "Paris", "Japan": "Tokyo", "Italy": "Rome"}, build a new dictionary with the keys and values swapped — {"Paris": "France", "Tokyo": "Japan", "Rome": "Italy"} — using a dict comprehension and .items().
Goal: Combine .items() iteration with a dict comprehension in one line.
Challenge 3: Safe Lookup Report
Given inventory = {"apples": 10, "bananas": 5} and a list of items to check, ["apples", "bananas", "cherries"], print a stock report line for each one — using .get() so a missing item like "cherries" prints "0 in stock" instead of crashing.
Goal: Practice defensive dictionary lookups with .get() instead of direct indexing.
🎯 What's Next
Next chapter: Functions — def, positional/keyword/default/*args/**kwargs parameters, return, and scope.