Iterating with dictionaries is one of the most common and useful forms of iteration in Python. Dictionaries are iterables — by default, a for loop iterates over their keys — but Python gives you flexible, readable ways to access keys, values, or both at the same time. In 2026, mastering dictionary iteration is essential for working with JSON data, API responses, configuration objects, dataframes metadata, and any key-value structure.
Here’s a complete, practical guide to iterating over dictionaries: default key iteration, explicit methods (.keys(), .values(), .items()), real-world patterns, and modern best practices with type hints and safety.
By default, a for loop over a dictionary yields its keys — you can look up values with dict[key]. This is simple but works well when you only need keys or want to access values manually.
fruits = {
"apple": 1.5,
"banana": 0.8,
"cherry": 3.0
}
for fruit in fruits:
print(f"{fruit.capitalize()}: ${fruits[fruit]:.2f}")
# Output:
# Apple: $1.50
# Banana: $0.80
# Cherry: $3.00
For most real code, .items() is the preferred way — it gives you both key and value in one step, unpacked directly into two loop variables. It’s clearer, avoids repeated lookups, and is slightly faster.
for fruit, price in fruits.items():
print(f"{fruit.capitalize()}: ${price:.2f}")
# Same output as above — but cleaner and more efficient
Use .keys() when you only need keys (rare — for key in dict is usually enough), and .values() when you only care about values (e.g., summing or finding min/max).
# Keys only (same as default iteration)
for key in fruits.keys():
print(key)
# Values only — great for stats
total = sum(fruits.values())
print(f"Total price: ${total:.2f}") # Total price: $5.30
min_price = min(fruits.values())
print(f"Cheapest: ${min_price:.2f}") # Cheapest: $0.80
Real-world pattern: processing JSON/API data or configuration dictionaries — you often need both key and value, and may want to filter or transform them.
config = {
"debug": True,
"port": 8080,
"host": "localhost",
"timeout": 30
}
# Print only non-default settings
for key, value in config.items():
if value != config.get(key): # Dummy check — in real code compare to defaults
print(f"Custom {key}: {value}")
# Build a filtered dict (comprehension style)
active_settings = {k: v for k, v in config.items() if v is not None}
print(active_settings)
Best practices make dictionary iteration clean, safe, and efficient. Prefer for key, value in dict.items() — it’s the most readable and performant way to access both. Avoid repeated dict[key] lookups inside loops — use .items() to unpack once. Use type hints for clarity — dict[str, float] or dict[str, Any] — especially in function signatures. When modifying a dictionary while iterating, never do it directly — collect changes in a new dict or list to avoid RuntimeError: dictionary changed size during iteration. Modern tip: use dictionary union ({**d1, **d2} or d1 | d2 in 3.9+) for merging while iterating. In production, wrap dictionary iteration over external data (API responses, JSON files) in try/except — handle missing keys or type mismatches gracefully.
Iterating over dictionaries with for loops and .items() is how you turn key-value data into results — cleanly and efficiently. In 2026, prefer .items() for key-value access, use comprehensions for filtering/transforming, and add type hints for safety. Master dictionary iteration, and you’ll handle JSON, configs, metadata, and API data with confidence and elegance.
Next time you have a dictionary — reach for for key, value in dict.items(). It’s Python’s cleanest, most powerful way to work with key-value pairs.