Python Loop through a Dictionary
Introduction
in this tutorial, we will discuss in python How to Loop Through a Dictionary.
In contrast to other Data Types, which can only retain a single value as an element, a Dictionary in Python can have a key: value pair. It is used to store the data values like a map.
In Python, there are numerous ways to iterate through a dictionary.
- Utilizing the build.keys(), access the key
- Without utilizing the key(), access the key
- loop via all values using.values()
- loop via all keys, and value pair utilizing items()
- value without utilizing items() and access both key
- Printing elements as a pair in Key-Value
Dictionary Refresher
Key-value structures are used to store data in Python dictionaries. This indicates that a key is given to each value that can be utilized to refer to that specific item.
Here is an illustration of a Python dictionary:
book = {
'Title': 'The Omnivore’s Dilemma: A Natural History of Four Meals',
'Author': 'MichaelPollan',
'Date_ published': 2006,
'In_stock': True
}
The keys and values in our dictionary are denoted by colons (:), which are used throughout. The keys, in this example title, author, date published, and in stock, are the words to the left of the colons. All of these keys have a string format.
Example 1: Utilizing the build.keys(), access the key
You'll see that in this example, we're using the in-built keys() method, which enables us to print every key in the dictionary.
Code:
# Iterating through each value in a dictionary using Python 3
states_And_Capitals = {
'Arunachal Pradesh' : 'Itanagar' ,
'Assam': 'Dispur' ,
'Bihar' : 'Patna' ,
'Chhattisgarh' : 'Raipur' ,
'Goa' : 'Panaji' ,
'Gujarat' : 'Gandhinagar' ,
'Haryana' : 'Chandigarh' ,
'Himachal Pradesh' : 'Shimla' ,
'Jharkhand' : 'Ranchi' ,
'Karnataka' : 'Bengaluru' ,
'Kerala' : 'Thiruvananthapuram' ,
'Madhya Pradesh' : 'Bhopal'
}
key = states_And_Capitals.keys()
print (key)
Output:
dict_keys(['Arunachal Pradesh', 'Assam', 'Bihar', 'Chhattisgarh', 'Goa', 'Gujarat', 'Haryana', 'Himachal Pradesh', 'Jharkhand', 'Karnataka', 'Kerala', 'Madhya Pradesh'])
Example 2: Without utilizing the key(), access the key
Using "for" loops to iterate over dictionaries allows us to iterate our keys and print every key found in the dictionary.
Code:
# Iterate through each key in a dictionary using Python 3
states_And_Capitals = {
'Arunachal Pradesh' : 'Itanagar' ,
'Assam': 'Dispur' ,
'Bihar' : 'Patna' ,
'Chhattisgarh' : 'Raipur' ,
'Goa' : 'Panaji' ,
'Gujarat' : 'Gandhinagar' ,
'Haryana' : 'Chandigarh' ,
'Himachal Pradesh' : 'Shimla' ,
'Jharkhand' : 'Ranchi' ,
'Karnataka' : 'Bengaluru' ,
'Kerala' : 'Thiruvananthapuram' ,
'Madhya Pradesh' : 'Bhopal'
}
print ('List Of given states:\n')
# Looping over keys
for state in states_And_Capitals:
print(state)
Output:
List Of given states:
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jharkhand
Karnataka
Kerala
Madhya Pradesh
Example 3: loop via all values using.values()
The values() method is being used in this example to print every value that is available in the dictionary.
Code:
# Iterating through each value in a dictionary using Python 3
states_And_Capitals = {
'Arunachal Pradesh' : 'Itanagar' ,
'Assam': 'Dispur' ,
'Bihar' : 'Patna' ,
'Chhattisgarh' : 'Raipur' ,
'Goa' : 'Panaji' ,
'Gujarat' : 'Gandhinagar' ,
'Haryana' : 'Chandigarh' ,
'Himachal Pradesh' : 'Shimla' ,
'Jharkhand' : 'Ranchi' ,
'Karnataka' : 'Bengaluru' ,
'Kerala' : 'Thiruvananthapuram' ,
'Madhya Pradesh' : 'Bhopal'
}
print ('List Of given capitals:\n')
# Looping over values
for capt in states_And_Capitals.values():
print (capt)
Output:
List Of given capitals:
Itanagar
Dispur
Patna
Raipur
Panaji
Gandhinagar
Chandigarh
Shimla
Ranchi
Bengaluru
Thiruvananthapuram
Bhopal
Example 4: loop via all keys, and value pair utilizing items()
In this example, we'll use the items() method to output every key and value pair found in a dictionary.
Code:
# Iterating through each key-value pair in a dictionary using Python 3
states_And_Capitals = {
'Arunachal Pradesh' : 'Itanagar' ,
'Assam': 'Dispur' ,
'Bihar' : 'Patna' ,
'Chhattisgarh' : 'Raipur' ,
'Goa' : 'Panaji' ,
'Gujarat' : 'Gandhinagar' ,
'Haryana' : 'Chandigarh' ,
'Himachal Pradesh' : 'Shimla' ,
'Jharkhand' : 'Ranchi' ,
'Karnataka' : 'Bengaluru' ,
'Kerala' : 'Thiruvananthapuram' ,
'Madhya Pradesh' : 'Bhopal'
}
print ('List of given states, together with their capitals:\n')
# Looping over values
for state, capt in states_And_Capitals.items():
print (state, ":", capt)
Output:
List of given states, together with their capitals:
Arunachal Pradesh : Itanagar
Assam : Dispur
Bihar : Patna
Chhattisgarh : Raipur
Goa : Panaji
Gujarat : Gandhinagar
Haryana : Chandigarh
Himachal Pradesh : Shimla
Jharkhand : Ranchi
Karnataka : Bengaluru
Kerala : Thiruvananthapuram
Madhya Pradesh : Bhopal
Example 5: value without utilizing items() and access both key
In this illustration, a Python Loop In via a Dictionary is used, and with each iteration, the directory's key is obtained. Next, key data is printed, and a key is used as an index to display values from the directory.
Code:
# Iterating through each value in a dictionary using Python 3
states_And_Capitals = {
'Arunachal Pradesh' : 'Itanagar' ,
'Assam' : 'Dispur' ,
'Bihar' : 'Patna' ,
'Chhattisgarh' : 'Raipur' ,
'Goa' : 'Panaji' ,
'Gujarat' : 'Gandhinagar' ,
'Haryana' : 'Chandigarh' ,
'Himachal Pradesh' : 'Shimla' ,
'Jharkhand' : 'Ranchi' ,
'Karnataka' : 'Bengaluru' ,
'Kerala' : 'Thiruvananthapuram' ,
'Madhya Pradesh' : 'Bhopal'
}
for j in states_And_Capitals:
print(j, '->', states_And_Capitals[j])
Output:
Arunachal Pradesh -> Itanagar
Assam -> Dispur
Bihar -> Patna
Chhattisgarh -> Raipur
Goa -> Panaji
Gujarat -> Gandhinagar
Haryana -> Chandigarh
Himachal Pradesh -> Shimla
Jharkhand -> Ranchi
Karnataka -> Bengaluru
Kerala -> Thiruvananthapuram
Madhya Pradesh -> Bhopal
Example 6: Printing elements as a pair in Key-Value
In this example, the key value data is printed as pairs and each pair is contained within a dictionary.
Code:
# Iterating through each value in a dictionary using Python 3
states_And_Capitals = {
'Arunachal Pradesh' : 'Itanagar' ,
'Assam': 'Dispur' ,
'Bihar' : 'Patna' ,
'Chhattisgarh' : 'Raipur' ,
'Goa' : 'Panaji' ,
'Gujarat' : 'Gandhinagar' ,
'Haryana' : 'Chandigarh' ,
'Himachal Pradesh' : 'Shimla' ,
'Jharkhand' : 'Ranchi' ,
'Karnataka' : 'Bengaluru' ,
'Kerala' : 'Thiruvananthapuram' ,
'Madhya Pradesh' : 'Bhopal'
}
key = states_And_Capitals.items()
print(key)
Output:
dict_items([('Arunachal Pradesh', 'Itanagar'), ('Assam', 'Dispur'), ('Bihar', 'Patna'), ('Chhattisgarh', 'Raipur'), ('Goa', 'Panaji'), ('Gujarat', 'Gandhinagar'), ('Haryana', 'Chandigarh'), ('Himachal Pradesh', 'Shimla'), ('Jharkhand', 'Ranchi'), ('Karnataka', 'Bengaluru'), ('Kerala', 'Thiruvananthapuram'), ('Madhya Pradesh', 'Bhopal')])
Conclusion
There are numerous methods you can employ in Python to iterate through a dictionary. This tutorial covered the four methods for iterating over a dictionary in Python. We also looked at a real-world application of each of these techniques.