Skip to content
Related Articles

Related Articles

Iterate over a dictionary in Python

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 01 Jul, 2022

In this article, we will cover How to Iterate Through a Dictionary in Python

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

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

  • Access key using the build .keys() 
  • Access key without using a key() 
  • Iterate through all values using .values()
  • Iterate through all key, and value pairs using items()
  • Access both key and value without using items()
  • Print items in Key-Value in pair 

Example 1: Access key using the build .keys() 

In this example, you will see that we are using an in-build. keys() method which helps us to print all the keys in the dictionary.

Python3




# Python3 code to iterate through all values in a dictionary
 
statesAndCapitals = {
    'Gujarat': 'Gandhinagar',
    'Maharashtra': 'Mumbai',
    'Rajasthan': 'Jaipur',
    'Bihar': 'Patna'
}
 
keys = statesAndCapitals.keys()
print(keys)

Output:

dict_keys(['Gujarat', 'Maharashtra', 'Rajasthan', 'Bihar'])

Example 2: Access key without using a key() 

Iterating over dictionaries using ‘for’ loops for iterating our keys and printing all the keys present in the Dictionary.

Python3




# 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:

Gujarat
Maharashtra
Rajasthan
Bihar

Example 3: Iterate through all values using .values()

In this example, we are using the values() method to print all the values present in the dictionary.

Python3




# 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
Mumbai
Jaipur
Patna

Example 4: Iterate through all key, and value pairs using items()

In this example, we are printing all the key and value pairs present in a dictionary using a items() method.

Python3




# 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:

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

Example 5: Access both key and value without using items()

In this example, we are using a Python Loop Through a Dictionary, and with each iteration, we are obtaining the key of the directory after that, we are printing key data and we are using a key as an index to print values from the directory

Python3




# Python3 code to iterate through all values in a dictionary
 
statesAndCapitals = {
    'Gujarat': 'Gandhinagar',
    'Maharashtra': 'Mumbai',
    'Rajasthan': 'Jaipur',
    'Bihar': 'Patna'
}
 
for i in statesAndCapitals:
    print(i, '->', statesAndCapitals[i])

Output:

Gujarat -> Gandhinagar
Maharashtra -> Mumbai
Rajasthan -> Jaipur
Bihar -> Patna

Example 6: Print items in Key-Value in pair 

In this example, we are printing the key values data in the form of pairs and all the pair are enclosed in a dictionary. 

Python3




# Python3 code to iterate through all values in a dictionary
 
statesAndCapitals = {
    'Gujarat': 'Gandhinagar',
    'Maharashtra': 'Mumbai',
    'Rajasthan': 'Jaipur',
    'Bihar': 'Patna'
}
 
keys = statesAndCapitals.items()
print(keys)

Output:

dict_items([('Gujarat', 'Gandhinagar'), ('Maharashtra', 'Mumbai'), 
('Rajasthan', 'Jaipur'), ('Bihar', 'Patna')])

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!