# Dictionaries — Python

Source: https://www.geekswithgeeks.com/en/python/py-dicts

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

```python
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.

```python
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.
