Iterating over dictionaries using 'for' loops
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Iterating over a Python dictionary is simple once you know what view you need: keys, values, or key-value pairs. Most confusion comes from using the wrong dictionary method for the job or from mutating the dictionary while looping over it.
The Default Loop Iterates Over Keys
When you loop over a dictionary directly, Python gives you the keys.
This is exactly the same as:
Use this when you only need the keys or when you will look up values manually.
Loop Over Values With .values()
If only the values matter, use .values().
This is clearer than looping over keys and then indexing back into the dictionary for each value.
Loop Over Both With .items()
If you need both the key and the value, .items() is usually the best choice.
This is the idiomatic pattern in Python and the one you will use most often in application code.
When Order Matters
Modern Python dictionaries preserve insertion order. That means iteration follows the order in which keys were added.
If you want alphabetical order or some other ordering rule, say so explicitly.
Do not rely on insertion order if the real requirement is sorted order.
Enumerating Dictionary Iteration
If you need a loop counter as well, wrap the dictionary view in enumerate.
This is useful for numbered reports or formatted output.
Filtering During Iteration
You can add conditions in the loop body just like any other for loop.
This is often clearer than creating a temporary filtered dictionary unless you actually need that filtered structure later.
Do Not Mutate While Iterating
A common bug is changing the size of the dictionary during iteration.
The important part is list(prices.keys()). That creates a snapshot so the loop is not invalidated by deletions.
If you mutate the dictionary directly while iterating over the live view, Python can raise a runtime error.
Nested Dictionaries
When values are dictionaries themselves, you can unpack step by step.
This pattern is common in JSON-like application data.
Common Pitfalls
The most common mistake is looping over a dictionary directly and forgetting that the loop variable is the key, not the value.
Another mistake is using .keys() and then repeatedly indexing into the dictionary when .items() would be clearer and simpler.
Developers also get into trouble by mutating the dictionary during iteration. If keys may be added or removed, iterate over a copied list of keys instead.
Finally, do not confuse insertion order with sorted order. If sorted output matters, call sorted explicitly.
Summary
- Looping over a dictionary directly gives you keys.
- Use
.values()when only values matter. - Use
.items()when you need both keys and values. - Use
enumerateorsortedwhen numbering or ordering is required. - Avoid changing the dictionary's size while iterating over its live views.
Related reading
- Iterating over every two elements in a list
- Iterating Through a Dictionary in Swift
- Iterating through a list in reverse order in java
- Iterating through dictionary with ForEach
- Iterating over tf.Tensor is not allowed AutoGraph is disabled in this function
- Iterating through a range of dates in Python
- Iteration order of HashSet
- Iterative deepening vs depth-first search

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.