In this article, we are going to learn 8 ways to Iterate a Python dictionary by value, by key, by key-value, nested dictionary. We will learn by using examples where we will access the key and values and print them line by line with different iteration programs
1. For loop to Iterate a dictionary Python Dictionary by key
This is our first example, we will make use of for loop to iterate over a dictionary in Python. We have a dictionary of Cricket players with their average of runs scored. We will iterate over this dictionary using for loop and print only the key
Here is the code to understand this.
Python Program Example
OneDayAverage= {
"Sachin": 56,
"Dravid" : 70 ,
"Sourav" : 80, "Dhoni" : 90
}
print('Print dictionary=',OneDayAverage,'\n')
print('iterate over dictionary using loop and key:\n')
for key in OneDayAverage:
print(key)
Output
Print dictionary= {'Sachin': 56, 'Dravid': 70, 'Sourav': 80, 'Dhoni': 90}
iterate over dictionary using loop and key:
Sachin
Dravid
Sourav
Dhoni
For loop to iterate over a dictionary key,value
In the above code example, we are using a loop to iterate over a dictionary key, but by using the same approach we can print both key and values, and shown below example.
OneDayAverage= {
"Sachin": 56,
"Dravid" : 70 ,
"Sourav" : 80, "Dhoni" : 90
}
print('Print dictionary=',OneDayAverage,'\n')
print('iterate over dictionary using loop and key:\n')
for key in OneDayAverage:
print(key,'--',OneDayAverage[key])
Output
iterate over dictionary using loop and key:
Sachin -- 56
Dravid -- 70
Sourav -- 80
Dhoni -- 90
2. OrderedDict() to iterate dictionary keys
Sometimes we want to iterate over the dictionary by the way values are inserted into the dictionary, OrderedDict, maintain the order of key-values as these are defined in the dictionary.
Python Program Example
from collections import OrderedDict
OneDayAverage = OrderedDict([
("Sachin", 56) ,
("Dravid" , 70),
("Sourav" , 80),
("Dhoni" , 90)
])
print('iterate over dictionary keys :\n')
for key in OneDayAverage:
print(key)
Output
iterate over dictionary keys :
Sachin
Dravid
Sourav
Dhoni
3. Keys() method to iterate a dictionary keys
The keys() function return the keys() of a dictionary.Here, we are iterating over a dictionary using a loop.
Python Program Example
OneDayAverage= {
"Sachin": 56,
"Dravid" : 70 ,
"Sourav" : 80, "Dhoni" : 90
}
print('iterate over dictionary keys :\n')
for value in OneDayAverage.keys():
print(value)
Output
iterate over dictionary keys :
Sachin
Dravid
Sourav
Dhoni
4. values() method to iterate dictionary values
The values() function return values of dictionary.Here we are using the values() function to iterate over the values of the dictionary.
Python Program Example
OneDayAverage= {
"Sachin": 56,
"Dravid" : 70 ,
"Sourav" : 80, "Dhoni" : 90
}
print('iterate over dictionary values using loop :\n')
for value in OneDayAverage.values():
print(value)
Output
iterate over dictionary values using loop :
56
70
80
90
5. items() method to Iterate over dictionary key-values
In this code example, we are using the items() method that return both keys, values of the dictionary and using for loop to iterate over key-values and printing output.
Python Program Example
OneDayAverage= {
"Sachin": 56,
"Dravid" : 70 ,
"Sourav" : 80,
"Dhoni" : 90
}
print('iterate over dictionary key- values using loop:\n')
for key, value in OneDayAverage.items():
print(key ,'-',value)
Output
iterate over dictionary key- values using loop:
Sachin - 56
Dravid - 70
Sourav - 80
Dhoni - 90
6. Iterate Python Dictionary in JSON format using Json.dump()
In this example we will be using the JSON.dump() function from the json package of python. This function will convert our dictionary in to json format and we will print it.
Python Program Example
import json
OneDayAverage= {
"Sachin": 56,
"Dravid" : 70,
"Sourav" : 80,
"Dhoni" : 90
}
print('printing dictionary in json format:\n')
print(json.dumps(OneDayAverage,indent=2))
Output
printing dictionary in json format:
{
"Sachin": 56,
"Dravid": 70,
"Sourav": 80,
"Dhoni": 90
}
7. Iterate Python nested dictionary line by line
In this example we will iterate and print nested dictionary line by line. To do this we will be using a sample dictionary with Employee names and their information as a nested dictionary in it.Let us understand this with the code example below:
Python Program Example
Employee ={'John':{'Salary':10000,
'Desigination':'Sr.Engg',
'Address':'CA'},
'Jack':{'Salary':20000,
'Desigination':'Sr.Mgr',
'Address':'CA'}}
for key, value in Employee.items():
print('\n')
print('------------',key, '------------------')
for key, value in value.items():
print(key, ' : ', value)
Output
------------ John ------------------
Salary : 10000
Desigination : Sr.Engg
Address : CA
------------ Jack ------------------
Salary : 20000
Desigination : Sr.Mgr
Address : CA
8.List Comprehension to iterate dictionary
In this code example, we are using list comprehension to loop over the dictionary key and value, list comprehension provides a short and fast way to iterate over the dictionary.
Python Program Example
OneDayAverage= {
"Sachin": 56,
"Dravid" : 70,
"Sourav" : 80,"Dhoni" : 90
}
print('List Comprehension for print the dictionary values=\n')
[print(key,':', value) for key, value in OneDayAverage.items()]
Output
List Comprehension for print the dictionary values=
Sachin : 56
Dravid : 70
Sourav : 80
Dhoni : 90
Conclusion:
In this post, We learned 8 ways to Iterate a Python dictionary useful and help you solve your problem.
Happy learning!!