Updated April 13, 2023
Introduction to Python Iterator Dictionary
Dictionary is the collection of the data used inside the curly bracket. Data in the dictionary is stored in the form of a key-value pair, where the key should be immutable and unique. We can fetch data from a dictionary using its key. Iteration means going through each and every value of a data collection like a list or dictionary. We can also call it looping through values. This iteration exists in every programming language, and they have different ways to do it.
Examples to Implement Python Iterator Dictionary
Below are the examples of Python Iterator Dictionary:
Example #1
Code:
data = {'color':'white','car':'audi','mobile':'apple'}
print(data['car'])
Output:
Explanation: In this way, we can iterate a single item of the dictionary using its key. If we know the key of the dictionary, then we can access any value of the dictionary. If we don’t know the key of the dictionary and want to access all the values, we can use loops to access the values. Let’s try to run for loop for this dictionary.
Example #2
Code:
data = {'color':'white','car':'audi','mobile':'apple'}
for item in data:
print(item)
Output:
Explanation: Now, in this case, you might have noticed that we are only getting the name of the key of the dictionary; we are not getting values. Because we are printing the ‘item’ and that is the key, now we have to modify the loop to access the values from the dictionary.
Example #3
Code:
data = {'color':'white','car':'audi','mobile':'apple'}
for item in data:
print('Key => '+item+' | Value => '+ data[item])
Output:
Now you can see that this time we got both the key and its values as expected.
How can we know if a Collection of Data is Iterable or Not?
We can use the dir function and pass an object inside it to check.
Example #1
Code:
print(dir({}))
We are using the dir method and passing an empty dictionary into it.
Output:
Explanation: Now you see that list of the function it has, you can also notice it has the ‘__iter__’ method. It means any method or function that returns ‘__iter__’ can be iterated using for loop or any other loops. Now we have how to access keys and how to access keys and value both. Let’s say I only want to access only values from the dictionary.
Example #2
Code:
data = {'color':'white','car':'audi','mobile':'apple'}
for item indata.values():
print(item)
Output:
Explanation: In the above program, we have used the same dictionary and loop with using the values method; in this case, it will return the values of the dictionary instead of the key. Similarly, we can also do it to get only the keys of the dictionary.
Example #3
Code:
data = {'color':'white','car':'audi','mobile':'apple'}
for item indata.keys():
print(item)
Output:
As you can see, this time, we have a keys function for the loop, and the loop has return only keys. We can also fetch the keys and values of the dictionary without using a loop. We have values and keys functions.
Example #4
Code:
data = {'color':'white','car':'audi','mobile':'apple'}
print(data.values())
Output:
As you can see, in the above program, we haven’t used any kind of loop. Instead, we have used the built values method of python, and it has returned all the values from the dictionary.
Example #5
Code:
data = {'color':'white','car':'audi','mobile':'apple'}
print(data.keys())
Output:
Similarly, in the above program, we have used the keys method, and it has returned all the keys of the dictionary.
Example #6
Code:
data = {'color':'white','car':'audi','mobile':'apple'}
for item in data.items():
print(item)
Output:
Explanation: In the above program, we are using an item function inside the for loop, and with the help of this function, it will print both key and values of the dictionary. We don’t need to put extra effort into fetching keys and values separately.
Example #7
Code:
data = {'color':'white','car':'audi','mobile':'apple'}
for key, value indata.items():
print(key, value)
Output:
This is also a very simple way to fetch the key and value from the dictionary.
Conclusion
Iteration means going through the data set’s values or collection of the data; it can be a list or dictionary or any data set. There are many different ways to fetch the dictionary’s key and values by using a loop or using some in-built methods.
Recommended Articles
We hope that this EDUCBA information on “Python Iterator Dictionary ” was beneficial to you. You can view EDUCBA’s recommended articles for more information.