Python Iterate over dictionaries Example
1. Introduction
In programming, to iterate means to go over the items in an element. And when you iterate, you visit the various items in the element performing a specified action. Consider you have a bag of balls and you want to iterate over the balls, you visit each ball in the bag and perform a specified action. That is basically what iterating means.
And to iterate over a dictionary means to go over the items in a dictionary. But before proceeding any further, what exactly is a dictionary in Python? A dictionary is an unordered key-value store. The keys in a dictionary are unique while the values can be unique or not. With dictionaries, you can choose to loop over the keys, the values or both the keys and values at the same time.
When you iterate over a dictionary by key, it means that you go through the keys in the dictionary one after the other, and when you iterate over the values in a dictionary, you go from one value to another. Same with iterating over key-value pair.
So let us begin by looking at the various ways to iterate over a dictionary.
2. Iterate Over Keys
To iterate over a dictionary by keys, let us consider the following example.
student_details = {'Name': 'AbdulBasit Kabir', 'Age': 32, 'Location': 'Abuja, Nigeria'}
If you directly run a for loop through the dictionary, you would be iterating over the keys in the dictionary. For example, consider the following:
for details in student_details: print(details)
And the output would be the following:
Name Age Location
You would get the same output if you run the for loop explicitly over the keys of the dictionary. To get the keys of the dictionary, you simply use keys()
method. Consider the following example:
for details in student_details.keys(): print(details)
And the output would be the following:
Name Age Location
You can also be interested in iterating over the keys in sorted order. That way, all you need is to call the sorted method in Python to sort the keys before you begin iteration. Consider when we sort the keys in student_details
before iterating as follows:
for details in sorted(student_details.keys()): print(details)
The output in this case would have the keys sorted alphabetically
Age Location Name
3. Iterate Over Values
Remember when we said dictionaries are made of unordered keys and values. We have learned how to iterate over keys. Now the next question to ask is whether we can iterate over values. And the answer is YES.
To iterate over values in a dictionary, using a for loop, we do the following:
for values in student_details.values(): print(values)
And the output is the following:
AbdulBasit Kabir 32 Abuja, Nigeria
If you would like to have the values sorted, you can call the sorted method right before you start printing. However, since our values are not all strings, we need to pass in a parameter to the method sorted
as follows:
for values in sorted(student_details.values(), key=str): print(values)
And the output would be as follows:
32 AbdulBasit Kabir Abuja, Nigeria
4. Iterate Over Keys and Values
Sometimes, you need to iterate not only on the keys or values but on both at the same time. Even in this situation, Python got you covered. The method items()
is invoked on the dictionary so that it returns both the key and value during each iteration and with that, you can iterate over it as follows:
for items in student_details.items(): print(items)
And the output would be as follows:
('Name', 'AbdulBasit Kabir') ('Age', 32) ('Location', 'Abuja, Nigeria')
To return both keys and values separately in each iteration, we can follow this pattern:
for key, value in student_details.items(): print('Key: ', key) print('Value: ', value)
And the output would be:
Key: Name Value: AbdulBasit Kabir Key: Age Value: 32 Key: Location Value: Abuja, Nigeria
And if you wish to print the key-value pair but sorted by key, then you can try the following:
for key, value in sorted(student_details.items()): print('Key: ', key) print('Value: ', value)
And the output would be as follows:
Key: Age Value: 32 Key: Location Value: Abuja, Nigeria Key: Name Value: AbdulBasit Kabir
5. Python Iterate over dictionaries – Conclusion
In this example, we have learned what it means to iterate in programming and also how to iterate over dictionaries in Python. We learned a little on what dictionaries are and how we can iterate over them by key, value and both key or value.
6. Download the Source Code
This was an example of how to Iterate over dictionaries in Python
You can download the full source code of this example here: Python Iterate over dictionaries Example