Python Dictionaries are used extensively in real-time. If you’re using dictionaries in your program, you should definitely know how to iterate over the dictionary.
Although this is simple, there is a lot of confusion in the community. This is because many dictionary functions have changed from Python 2 to Python 3.
In this article, let’s check the different methods used to loop over a dictionary in Python.
Note: All the methods described below work for Python versions 3+.
Table of Contents
Method 1: Using the dictionary directly
Python Dictionaries can be iterated directly. However, it is important to note that, it would loop over the keys and not the values. For example, consider the below code snippet.
dictionary={'a':1,'b':2,'c':3,'d':4,'e':5} for key in dictionary: print(key)
Output:
a b c d e
If you want to access the values from the dictionary, simply use dictionary[key] as shown below.
dictionary={'a':1,'b':2,'c':3,'d':4,'e':5} for key in dictionary: print(dictionary[key])
Output:
1 2 3 4 5
Method 2: Using dict.keys()
From Python 3+, we can make use of dict.keys() function to loop over the dictionary keys. This returns a dynamic view of the keys in the dictionary.
Programmatically, this function returns a dict_keys object. You can think of it as a set-like object that represents a view of the dictionary’s keys.
dictionary={'a':1,'b':2,'c':3,'d':4,'e':5} print(dictionary.keys())
Output:
dict_keys(['a', 'b', 'c', 'd', 'e'])
We can loop over this object as shown below:
dictionary={'a':1,'b':2,'c':3,'d':4,'e':5} for key in dictionary.keys(): print(key)
Output:
a b c d e
Method 3: Using dict.values()
If you want to loop over just the dictionary’s values, you can use dict.values(). This returns a dynamic view of the values of a dictionary.
dict.values() returns a dict_values object. This is a set-like object that represents a view of dictionary’s values as shown below.
dictionary={'a':1,'b':2,'c':3,'d':4,'e':5} print(dictionary.values())
Output:
dict_values([1, 2, 3, 4, 5])
We can loop over the dict_values object to obtain just the values from the dictionary.
dictionary={'a':1,'b':2,'c':3,'d':4,'e':5} for value in dictionary.values(): print(value)
Output:
1 2 3 4 5
Method 4: Using dict.items()
What if you want to loop over keys and values of a dictionary? You can use dict.items()
This function returns a set-like object dict_items.
dictionary={'a':1,'b':2,'c':3,'d':4,'e':5} print(dictionary.items())
Output:
dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])
This can be iterated over as shown below.
dictionary={'a':1,'b':2,'c':3,'d':4,'e':5} for ele in dictionary.items(): print(ele)
Output:
('a', 1) ('b', 2) ('c', 3) ('d', 4) ('e', 5)
Conclusion
This brings us to the end of this article. We hope this article has been informative. Did you like this article? Do come back to us for more interesting content.
Which method did you like the most? Do comment and let us know.
Also check, How to read numbers from user input in Python?
Thank you for reading.
2 thoughts on “4 Different ways to loop over Python Dictionaries”