Use for key in d to visit dictionary keys, for value in d.values() to visit values, and for key, value in d.items() when you need both. A dictionary’s iteration order is insertion order in Python 3.7 and later.
Choose a loop based on what you need
For example, start with a dictionary mapping names to scores:
As an Amazon Associate I earn from qualifying purchases.
scores = {"Ada": 95, "Lin": 88}
| Loop form | What it yields | Use it when |
|---|---|---|
for key in d |
Keys | You need keys, or want to look up each value with d[key]. |
for value in d.values() |
Values | You do not need the keys. |
for key, value in d.items() |
Key/value pairs, unpacked into two variables | You need each key alongside its value. |
for key in sorted(d) |
Keys in sorted order | You want sorted traversal instead of insertion order. |
Loop over keys
A dictionary is directly iterable, and a plain loop visits its keys. Use the key to retrieve the corresponding value when needed:
for name in scores:
print(name, scores[name])
This is equivalent to looping over scores.keys() when you specifically want to make the keys view explicit.
#1 Best Overall
Loop over values
Call values() when the keys are irrelevant:
for score in scores.values():
print(score)
Loop over keys and values together
Call items() to get each entry as a (key, value) pair. Python can unpack each pair directly into loop variables:
for name, score in scores.items():
print(name, score)
What order does dictionary iteration use?
Python guarantees insertion order for dictionary iteration starting with Python 3.7. The Python 3.6 CPython implementation also preserved insertion order, but that behavior was an implementation detail rather than a language guarantee.
Rank #2
Changing the value of an existing key leaves that key in its original position. If you delete a key and later add it again, it appears at the end of the iteration order.
How to iterate over keys in sorted order
Pass the dictionary to sorted() to iterate through its keys in sorted order:
for key in sorted(scores):
print(key, scores[key])
sorted(scores) returns a sorted list of keys; it does not rearrange the dictionary or change its insertion order. Sorting requires the keys to be comparable with one another.
Can you change a dictionary while looping over it?
The keys(), values(), and items() methods return dynamic views that reflect changes to the underlying dictionary. Avoid adding or deleting entries while iterating over one of these views: Python’s documentation warns that doing so may raise a RuntimeError or cause the iteration to miss entries. If you need to change which entries exist, iterate over a separate list of keys, such as list(d), and make changes to the dictionary in that loop.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →




