Introduction
A dictionary in Python is a collection of key-value pairs, where each key is unique & associated with a specific value. Dictionaries are powerful data structures that allow you to store & retrieve data efficiently.

In this article, we will discuss different ways to iterate over a dictionary in Python. We will talk about the techniques like using the keys() method, values() method, items() method, and more.
How to Iterate Through a Dictionary in Python
1. Iterate Python dictionary using build.keys()
The keys() method returns a view object that contains all the keys of the dictionary. You can iterate over this view object using a for loop to access each key individually.
Example
Output
a
b
c
In this example, we iterate over the keys of the dictionary my_dict using the keys() method. The for loop assigns each key to the variable key, and we print the key in each iteration.
2. Iterate through all values using .values()
Similar to the keys() method, the values() method returns a view object containing all the values of the dictionary. You can use a for loop to iterate over the values directly.
Example:
Output
1
2
3
In this example, we use the values() method to obtain a view object of all the values in the dictionary my_dict. The for loop assigns each value to the variable value, and we print the value in each iteration.
3. Looping through Python Dictionary using for loop
You can directly iterate over a dictionary using a for loop. By default, the for loop iterates over the keys of the dictionary.
Example:
Output
a 1
b 2
c 3
In this example, we use a for loop to iterate over the keys of the dictionary my_dict. The loop variable key takes on each key value in each iteration. We then use the key to access the corresponding value in the dictionary using my_dict[key] and print both the key and value.
4. Iterate key-value pair using items()
The items() method returns a view object that contains key-value pairs as tuples. You can use a for loop to iterate over these tuples and access both the key and value simultaneously.
Example:
Output
a 1
b 2
c 3
In this example, we use the items() method to obtain a view object of key-value pairs as tuples. The for loop unpacks each tuple into the variables key and value, allowing us to access both the key and value in each iteration. We then print the key and value.
5. Access key Using map() and dict.get
You can use the built-in map() function along with the get() method of a dictionary to access keys and their corresponding values.
Example:
Output
[1, 2, 3]In this example, we have a list of keys called keys and a dictionary my_dict. We use the map() function to apply the get() method of the dictionary to each key in the keys list. The get() method retrieves the corresponding value for each key. Finally, we convert the map object to a list using list() and print the values.
6. Access key in Python Using zip()
The zip() function allows you to combine two or more iterables into tuples. You can use it to iterate over the keys and values of a dictionary simultaneously.
Example:
Output
a 1
b 2
c 3
In this example, we have two lists: keys and values. We use the zip() function to combine the keys and values into tuples and then convert the zip object into a dictionary using dict(). The resulting dictionary my_dict contains the key-value pairs. We then use the items() method to iterate over the key-value pairs and print them.
7. Access key Using Unpacking of Dict
You can unpack a dictionary into individual variables using the ** operator. This allows you to access the keys and values of the dictionary directly.
Example:
Output
a
b
c
In this example, we have a dictionary my_dict. We use the ** operator to unpack the dictionary into individual variables a, b, and c. Each variable corresponds to a key in the dictionary. We then print the values of a, b, and c, which are the keys of the dictionary.
Note: Always remember, that this method only works if the number of variables matches the number of keys in the dictionary.




