Back
+10 XP
Concept

Iterating Dictionaries: `.items()` Supremacy

Loop through dictionaries using `.items()` to get both the key and the value unpacked simultaneously in each iteration.

python
scores = {"Alice": 95, "Bob": 80}
for name, score in scores.items():
    print(f"{name}: {score}")

# 83. Iterating Dictionaries: `.items()` Supremacy Loop through dictionaries using `.items()` to get both the key and the value unpacked simultaneously in each iteration: ```python scores = {"Alice": 95, "Bob": 80} for name, score in scores.items(): print(f"{name}: {score}") ``` If you just write `for x in scores:`, Python only gives you the keys (`"Alice"`, `"Bob"`). Using `.items()` gives you full access to both keys and values cleanly.

Checkpoint

Which dictionary method yields `(key, value)` pairs for looping?

Practice

Iterate through `config = {"port": 8080, "debug": True}` using `.items()`. Print each `key, value` on its own line.

Terminal
01
Tap code blocks below to assemble
Available Blocks