Iterate over a dictionary in Python

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 
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'])
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 keysfor state in statesAndCapitals:    print(state)
Output:
List Of given states:

Gujarat
Maharashtra
Rajasthan
Bihar
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 valuesfor capital in statesAndCapitals.values():    print(capital)
Output:
List Of given capitals:

Gandhinagar
Mumbai
Jaipur
Patna
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 valuesfor state, capital in statesAndCapitals.items():    print(state, ":", capital)
Output:
List Of given states and their capitals:

Gujarat : Gandhinagar
Maharashtra : Mumbai
Rajasthan : Jaipur
Bihar : Patna
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
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')])
surajkumarguptaintern
Picked
python-dict
Technical Scripter 2018
Python
Technical Scripter
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Convert integer to string in Python
Introduction To PYTHON