Python theorytheory 0/50 · 0%
Collections · easy

7. Dictionaries

Key-value mappings, insertion-ordered since 3.7.

Dictionaries map hashable keys to values and preserve insertion order (guaranteed since Python 3.7).

python
wallet = {"address": "0xabc", "balance": 3.5}
wallet["balance"] += 1
wallet.get("nonce", 0)     # default if missing
"address" in wallet         # membership test on keys

Iterate with `.items()`, `.keys()`, or `.values()`:

python
for key, value in wallet.items():
    print(key, value)

Dict comprehensions mirror list comprehensions: `{k: v * 2 for k, v in prices.items()}`.

Check your understanding

  1. 1. What does `wallet.get('nonce', 0)` do if 'nonce' is missing?

  2. 2. Since which Python version is dict order guaranteed?

  3. 3. What does `"address" in wallet` test?