Dictionaries

Python Fundamentals — Dictionaries
Python Fundamentals
Course 1 · Chapter 7 · Dictionaries

📖 Dictionaries

A dictionary stores key-value pairs — the tool you reach for whenever you need to look something up by a name rather than a position. This chapter covers creating and accessing dictionaries, modifying them, the three ways to iterate over one, and a first dict comprehension.

🔑 Creating and Accessing Dictionaries

person = { "name": "Ada", "age": 28, "language": "Python" } print(person["name"]) # Ada print(person.get("age")) # 28 print(person.get("email", "unknown")) # unknown — default if key is missing
⚠ Direct Indexing Raises KeyError on a Missing Key
person["email"] # KeyError: 'email' — crashes the program

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

person["email"] = "ada@example.com" # adds a new key person["age"] = 29 # overwrites an existing key del person["language"] # removes a key entirely email = person.pop("email") # removes AND returns the value print("age" in person) # True — membership testing checks KEYS

🔁 Iterating Over Dictionaries

scores = {"Alice": 90, "Bob": 85, "Charlie": 78} for name in scores.keys(): print(name) # Alice, Bob, Charlie for score in scores.values(): print(score) # 90, 85, 78 for name, score in scores.items(): print(f"{name}: {score}") # Alice: 90, Bob: 85, Charlie: 78

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

LanguageTypeMissing key behavior
Gomap[string]intReturns 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.
KotlinMap<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.
Pythondictd[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 ...}:

numbers = [1, 2, 3, 4] squares = {n: n ** 2 for n in numbers} print(squares) # {1: 1, 2: 4, 3: 9, 4: 16} # with a filtering condition: even_squares = {n: n ** 2 for n in numbers if n % 2 == 0} print(even_squares) # {2: 4, 4: 16}

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.

→ Solution

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.

→ Solution

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.

→ Solution

🎯 What's Next

Next chapter: Functionsdef, positional/keyword/default/*args/**kwargs parameters, return, and scope.