In Python, the dictionary is an unordered dataset i.e., a collection of key-value pairs. Dictionary is also known as an associative array, hashmap, or simply a dict. Dicionary is mutable, which means that it allows us to make amends in it while execution. The keys stored in a dictionary must be unique, and the values stored in the dictionary can be of any data type. Dictionary stores data in such a way that it allows us to perform different operations, e.g., insertion and deletion in an efficient way.
Let's have a look at the following example to understand dictionaries better:
data = {"name": "educative","answer name": "How To Iterate Over a Dictionary in Python","contributor name": "Nimra Mubashir"}
In the above example, we have a dictionary named the data
. This dictionary consists of the following keys which are name
, answer name
, and contributor name
with educative
, How To Iterate Over a Dictionary in Python
, and Nimra Mubashir
as their corresponding values.
In Python, we have multiple ways to iterate over a dictionary. Some of these include, but are not limited to the following:
Using the for
loop to iterate over keys
Using the for
loop and the items
method to iterate over keys and values.
Using the for
loop and the values
method to iterate over the values.
Let's have a look at the following example to understand how we can iterate over the dictionary. The widget below contains the data
dictionary containing key1
, key2
, and key3
as the keys and value1, value2
, and value3
as the values and iterate over them using the above-mentioned methods.
data = { "key1": "value1","key2": "value2","key3": "value3"}def iterate_using_for_loop(my_dict):for key in my_dict:print("Key:", key)def iterate_using_items_method(my_dict):for key, value in my_dict.items():print(f"Key: {key}, Value:", value)def iterate_using_values_method(my_dict):for value in my_dict.values():print("Value:",value)print("Iterating over the keys of the dictionary:")iterate_using_for_loop(data)print (" ***************** ")print("Iterating over the keys and values of the dictionary:")iterate_using_items_method(data)print (" ***************** ")print("Iterating over the values of the dictionary:")iterate_using_values_method(data)
The explanation of the above code is provided below:
Lines 1–4: We're creating a dictionary named data
containing the key-value pairs.
Lines 6,10 and 14: We're creating the respective functions to depict different methods to iterate over a dictionary. These functions take the my_dict
dictionary as a parameter.
Lines 7,11 and 15: We're iterating over the dictionary using the for
loop. When we iterate over a dictionary using a single for
loop, it returns us the keys of the dictionary like in line 7. When we use the items()
method, it returns us both the keys as well as the values of the dictionary like in line 11. When we iterate using a for
loop and the values
method, it returns us the values of the dictionary like in line 15.
Lines 8,12 and 16: We're printing the keys, and values respectively.
Lines 18–25: We're calling the respective functions and printing the respective statements.
Free Resources