Lesson 13 / 47
Dictionaries
Key → value mappings, Python's workhorse for structured data.
Key → value pairs
A dict maps unique keys to values. Since 3.7, insertion order is preserved.
person = {"name": "Ada", "age": 36}
person["city"] = "London"
print(person.get("age"))
print(person.get("country", "unknown"))Iterating a dict
Loop over keys, values, or both with .items() — the most common dict pattern.
prices = {"pen": 10, "book": 250}
for item, price in prices.items():
print(item, "->", price)Avoiding KeyError
dict.get(key, default) avoids a KeyError for missing keys — safer than dict[key] when unsure.