Dictionaries are one of the most frequently used data structures in python. It contains data in the form of key value pairs. While processing the data with dictionaries, we may need to iterate over the items in the dictionary to change the values or read the values present in the dictionary. In this article, we will see various ways for iterating over dictionary in python.

Iterating over a dictionary using for loop

As we iterate through lists or tuples using for loops in python, we can also iterate  through a python dictionary using a for loop.

When we try to iterate over a dictionary using for loop,it implicitly calls __iter__() method. The __iter__() method returns an iterator with the help of which we can iterate over the entire dictionary. As we know that dictionaries in python are indexed using keys, the iterator returned by __iter__() method iterates over the keys in the python dictionary.

So, with a for loop, we can iterate over and access all the keys of a dictionary as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
print("The keys in the dictionary are:")
for x in myDict:
    print(x)

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The keys in the dictionary are:
name
acronym
about

In the output we can see that all the the all the keys have been printed. In the for loop the iterator x iterates over all the keys in the dictionary which are then printed.

Having obtained the keys of the dictionary using for loop, we can also iterate over the values in the dictionary as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
print("The values in the dictionary are:")
for x in myDict:
    print(myDict[x])

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The values in the dictionary are:
PythonForBeginners
PFB
Python Tutorials Website

In the code above, we have simply obtained an iterator which iterates over the keys in the and then we have accessed the values associated to the keys using the syntax dict_name[key_name] and then the values are printed.

Iterate over keys of a dictionary

We can use the keys() method to iterate over keys of a dictionary. The keys() method returns a list of keys in the dictionary when invoked on a dictionary and then we can iterate over the list to access the keys in the dictionary as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
print("The keys in the dictionary are:")
keyList=myDict.keys()
for x in keyList:
    print(x)

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The keys in the dictionary are:
name
acronym
about

We can also access values associated with the keys of the dictionary once we have the keys in the list as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
print("The values in the dictionary are:")
keyList=myDict.keys()
for x in keyList:
    print(myDict[x])

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The values in the dictionary are:
PythonForBeginners
PFB
Python Tutorials Website

Iterate over values of a dictionary in python

If we only want to access the values in the dictionary, we can do so with the help of the values() method.The values() method when invoked on a dictionary returns a list of all the values present in the dictionary. We can access the values in the dictionary using values() method and for loop as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
print("The values in the dictionary are:")
valueList=myDict.values()
for x in valueList:
    print(x)

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The values in the dictionary are:
PythonForBeginners
PFB
Python Tutorials Website

In the output, we can see that all the values present in the dictionary are printed one by one.

Iterating over items in a dictionary in python

We can iterate over and access the key value pairs using the items() method. The items() method when invoked on a dictionary, returns a list of tuples which have keys and values as pairs. Each tuple has a key on its 0th index and the value associated with the key is present on the 1st index of the tuple. We can access the key value pairs using items() method as shown below.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
print("The items in the dictionary are:")
itemsList=myDict.items()
for x in itemsList:
    print(x)

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The items in the dictionary are:
('name', 'PythonForBeginners')
('acronym', 'PFB')
('about', 'Python Tutorials Website')

Along with iterating the items in the dictionary using the items() method,we can also iterate over keys and values of  a dictionary using two iterators in the for loop as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
itemList=myDict.items()
print("The key value pairs in the dictionary are:")
for x,y in itemList:
    print(x,end=":")
    print(y)

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The key value pairs in the dictionary are:
name:PythonForBeginners
acronym:PFB
about:Python Tutorials Website

In the above program, the first iterator iterates over the keys in the dictionary and the second iterator iterates over the respective values associated with those keys which are present in the form of tuple containing key value pairs in the list returned by items() method.

Conclusion

In this article, we have seen various ways to iterate over the data in dictionaries. We have seen how to access the keys and values of the dictionary using for loop and inbuilt methods like keys()values() and items(). We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way. Stay tuned for more informative articles.

Leave a Reply

Your email address will not be published. Required fields are marked *