Skip to content
Related Articles

Related Articles

Improve Article
Iterate over a dictionary in Python
  • Difficulty Level : Medium
  • Last Updated : 15 Nov, 2018

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair.

There are multiple ways to iterate over a dictionary in Python.

  • Iterate through all keys
  • Iterate through all values
  • Iterate through all key, value pairs

Iterate through all keys:

The order of states in the below code will change every time because the dictionary doesn’t store keys in a particular order.




# Python3 code to iterate through all keys in a dictionary
  
statesAndCapitals = {
                     'Gujarat' : 'Gandhinagar',
                     'Maharashtra' : 'Mumbai',
                     'Rajasthan' : 'Jaipur',
                     'Bihar' : 'Patna'
                    }
                      
print('List Of given states:\n')
  
# Iterating over keys
for state in statesAndCapitals:
    print(state)
Output:



List Of given states:

Rajasthan
Bihar
Maharashtra
Gujarat

In order to maintain the order of keys and values in a dictionary, use OrderedDict.




# Python3 code to iterate through all keys 
# in a dictionary in a specific order
  
from collections import OrderedDict
  
statesAndCapitals = OrderedDict([
                                 ('Gujarat', 'Gandhinagar'),
                                 ('Maharashtra', 'Mumbai'),
                                 ('Rajasthan', 'Jaipur'),
                                 ('Bihar', 'Patna')
                                ])
                      
print('List Of given states:\n')
  
# Iterating over keys
for state in statesAndCapitals:
    print(state)
Output:
List Of given states:

Gujarat
Maharashtra
Rajasthan
Bihar

Above dictionary is an OrderedDict as the keys and values are stored in the order in which they were defined in the dictionary. Hence the above method should be used when we want to maintain the order of (key, value) stored in the dictionary.

 
Iterate through all values:

Again, in this case, the order in which the capitals are printed in the below code will change every time because the dictionary doesn’t store them in a particular order.




# Python3 code to iterate through all values in a dictionary
  
statesAndCapitals = {
                     'Gujarat' : 'Gandhinagar',
                     'Maharashtra' : 'Mumbai',
                     'Rajasthan' : 'Jaipur',
                     'Bihar' : 'Patna'
                    }
                      
print('List Of given capitals:\n')
  
# Iterating over values
for capital in statesAndCapitals.values():
    print(capital)
Output:
List Of given capitals:

Gandhinagar
Jaipur
Mumbai
Patna

 

Iterate through all key, value pairs:




# Python3 code to iterate through all key, value 
# pairs in a dictionary
  
statesAndCapitals = {
                     'Gujarat' : 'Gandhinagar',
                     'Maharashtra' : 'Mumbai',
                     'Rajasthan' : 'Jaipur',
                     'Bihar' : 'Patna'
                    }
                      
print('List Of given states and their capitals:\n')
  
# Iterating over values
for state, capital in statesAndCapitals.items():
    print(state, ":", capital)
Output:
List Of given states and their capitals:

Bihar : Patna
Gujarat : Gandhinagar
Rajasthan : Jaipur
Maharashtra : Mumbai

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :