How to Iterate through a Dictionary in Python
In this tutorial, we will learn how to interate throught the dictionary in the Python programming, using different approaches with example.
Introduction to Dictionary in Python
Dictionary is a special type of data structure in Python. It is an unordered collection of data, with data being stored with a key and value pairs. Dictionaries in Python can be created by giving them a name, and the key-value pairs can be declared inside the curly braces {}. There is no limit on the number of key-value pairs expressed in a single dictionary; there can be as many as a minimum being one to a maximum n number of key-value pairs. The value of the key in a dictionary can be of any data type, with no restriction on that part.
A comma can differentiate each key-value pair. The unique thing about dictionaries is that the key-value pair can be duplicated as often as we want. An important point to remember is that the Dictionary, when declared twice with the same variable name, then the Dictionary that is stated at the end will be printed when the print function is used on them. There is no compulsory rule that a dictionary must always be declared with key-value pair in it; we can even declare an empty dictionary and then pass values later in future too.
The key-value pair then can be pushed by passing through the index number; also, the values and keys can be accessed using the index number. Accessing the elements can be accessed using the get() function. This get function returns the value of the element when the key of the particular key is passed as an argument inside the braces of the get function. One of the other most popular methods to access the elements is using the loop, either a for loop or a while loop.
See the below code for reference for creating a sample dictionary.
Code:
sample_dictionary = {
'Name' : 'javatpoint',
'location':'internet',
'achievement':'best website to learn programming'
}
print(sample_dictionary)
Output
{'Name': 'javatpoint', 'location': 'internet', 'achievement': 'best website to learn programming'}
How to Iterate using different methods through a Dictionary in Python
There are multiple ways and built-in methods to iterate through a dictionary in Python.
Some of them are given below:
1. using the build keys function.
2. without using the build key function.
3. using the dot values function.
4. iterating through key-value pairs using the items function.
5. without using the items function to iterate through the key-value pairs.
6. Printing the elements of the dictionary which are (key-value) pair.
Let us now discuss each method to iterate through a dictionary in detail.
Using the Build-Keys() Function Method:
Observe the below code carefully
Code
StatesAndCapitals = {
'Telangana' : 'Hyderabad',
'Tamilnadu' : 'Chennai'
}
keys = StatesAndCapitals
print(keys)
Output
{'Telangana': 'Hyderabad', 'Tamilnadu': 'Chennai'}
Exaplanation:
Here in the above code, we see variable named states and capitals is created as a dictionary in which two elements are created. The key here is the states' names, and the values are the capitals of the respective states. After the Dictionary is made, another variable named keys is declared in which the name of the dictionary dot keys function is declared. When the newly created function is printed, which is key in this case as the name rightly tells when the print function is executed, the keys of the Dictionary are printed on the output screen.
Without using the Build Key Function
If we are not using the built-in functions in the Python programming language, then the only possible way is by iterating the Dictionary using a for or a while loop.
See the below code for reference to iterate through a dictionary
Code:
StatesAndCapitals = {
'Telangana' : 'Hyderabad',
'Tamilnadu' : 'Chennai'
}
for states in StatesAndCapitals:
print(states)
Output
Telangana
Tamilnadu
Using the Dot Values Function.
In the above two methods, we have seen the keys being printed, but by using the dot values function, we can print the values of the Dictionary.
See the below code for reference.
Code
StatesAndCapitals = {
'Telangana' : 'Hyderabad',
'Tamilnadu' : 'Chennai'
}
for states in StatesAndCapitals.values() :
print(states)
Output
Hyderabad
Chennai
Iterating through Key-Value Pairs using the Items Function
In the above cases, we have seen how to print keys and values of the Dictionary separately, but by using the items function in Python, we can print both of them together.
See the below code for reference:
Code
StatesAndCapitals = {
'Telangana' : 'Hyderabad',
'Tamilnadu' : 'Chennai'
}
for states in StatesAndCapitals.items() :
print(states)
Output
('Telangana', 'Hyderabad')
('Tamilnadu', 'Chennai')
Without using the Items Function to Iterate through the Key-value Pairs
As an optimization to the above way to print the key-value pair, we can also iterate through the Dictionary using the for loop.
See the below code for reference to iterate through the dictionary.
Code
StatesAndCapitals = {
'Telangana' : 'Hyderabad',
'Tamilnadu' : 'Chennai'
}
for i in StatesAndCapitals :
print(i , 'capital is' , StatesAndCapitals[i])
Output
Telangana capital is Hyderabad
Tamilnadu capital is Chennai
Printing the Elements of the Dictionary (Key-Value) Pair
As we have seen in the above cases, we have printed the key-value pair using a built-in function and a for loop separately; we will combine both.
See the below code for reference.
Code
StatesAndCapitals = {
'Telangana' : 'Hyderabad',
'Tamilnadu' : 'Chennai'
}
keys = StatesAndCapitals.items()
print(keys)
Output
dict_items([('Telangana', 'Hyderabad'), ('Tamilnadu', 'Chennai')])