Iterating Dictionaries in Python: Keys, Values, Items, enumerate, Conditional Filtering, and Comprehensions
This guide demonstrates six common ways to iterate over Python dictionaries—including traversing keys, values, key‑value pairs, using enumerate for indexed access, applying conditional filters within loops, and employing dict comprehensions for efficient transformation—while explaining the appropriate use cases and performance considerations.
1. Iterate Keys
If you only need to access dictionary keys, you can use either the keys() method or iterate directly over the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
# Method 1: using keys()
for key in my_dict.keys():
print(key)
# Method 2: direct iteration (more concise)
for key in my_dict:
print(key)Applicable scenario: when you only need to handle the keys.
2. Iterate Values
If you only need to access dictionary values, you can use the values() method or iterate over items() while ignoring the keys.
my_dict = {'a': 1, 'b': 2, 'c': 3}
# Method 1: using values()
for value in my_dict.values():
print(value)
# Method 2: using items() and discarding the key
for _, value in my_dict.items():
print(value)Applicable scenario: when you only need to handle the values.
3. Iterate Keys and Values (items)
When you need both keys and values, use the items() method.
my_dict = {'a': 1, 'b': 2, 'c': 3}
# Using items()
for key, value in my_dict.items():
print(f"Key: {key}, Value: {value}")Applicable scenario: when you need to process both keys and values.
4. Use enumerate for indexed key‑value pairs
If you also need the current index during iteration, combine enumerate() with items() .
my_dict = {'a': 1, 'b': 2, 'c': 3}
# Using enumerate with items()
for index, (key, value) in enumerate(my_dict.items()):
print(f"Index: {index}, Key: {key}, Value: {value}")Applicable scenario: when you need keys, values, and their indices.
5. Conditional filtering within dict.items()
You can add a condition inside the loop to filter items on the fly.
my_dict = {'a': 1, 'b': 2, 'c': 3}
# Print only pairs where the value is greater than 1
for key, value in my_dict.items():
if value > 1:
print(f"Key: {key}, Value: {value}")Applicable scenario: when you need to perform conditional checks while iterating.
6. Use dict comprehension for efficient transformation or filtering
When you want to create a new dictionary with transformed or filtered data, a dict comprehension is concise and fast.
my_dict = {'a': 1, 'b': 2, 'c': 3}
# Generate a new dict containing only items with value > 1
new_dict = {key: value for key, value in my_dict.items() if value > 1}
print(new_dict)Applicable scenario: when you need a new dictionary after applying conversion or filtering logic.
Performance considerations
The keys() , values() , and items() methods return view objects rather than copies, making them memory‑efficient. Direct iteration over the dictionary (e.g., for key in my_dict: ) is among the fastest approaches because it leverages native iteration support.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.