Table of contents
1.
Introduction
2.
How to Iterate Through a Dictionary in Python
2.1.
1. Iterate Python dictionary using build.keys()
2.2.
Python
2.3.
2. Iterate through all values using .values()
2.4.
Python
2.5.
3. Looping through Python Dictionary using for loop
2.6.
Python
2.7.
4. Iterate key-value pair using items()
2.8.
Python
2.9.
5. Access key Using map() and dict.get
2.10.
Python
2.11.
6. Access key in Python Using zip()
2.12.
Python
2.13.
7. Access key Using Unpacking of Dict
2.14.
Python
3.
Frequently Asked Questions
3.1.
Can I modify a dictionary while iterating over it?
3.2.
What happens if I try to access a key that doesn't exist in the dictionary?
3.3.
Can I iterate over a dictionary in a specific order?
4.
Conclusion
Last Updated: Aug 13, 2025
Easy

Iterate Over a Dictionary in Python

Author Pallavi singh
0 upvote

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. 

Iterate Over a Dictionary in Python

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

  • Python

Python

my_dict = {'a': 1, 'b': 2, 'c': 3}

for key in my_dict.keys():

   print(key)
You can also try this code with Online Python Compiler
Run Code


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:

  • Python

Python

my_dict = {'a': 1, 'b': 2, 'c': 3}

for value in my_dict.values():

   print(value)
You can also try this code with Online Python Compiler
Run Code


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:

  • Python

Python

my_dict = {'a': 1, 'b': 2, 'c': 3}

for key in my_dict:

   print(key, my_dict[key])
You can also try this code with Online Python Compiler
Run Code


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:

  • Python

Python

my_dict = {'a': 1, 'b': 2, 'c': 3}

for key, value in my_dict.items():

   print(key, value)
You can also try this code with Online Python Compiler
Run Code


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:

  • Python

Python

keys = ['a', 'b', 'c']

my_dict = {'a': 1, 'b': 2, 'c': 3}

values = list(map(my_dict.get, keys))

print(values)
You can also try this code with Online Python Compiler
Run Code


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:

  • Python

Python

keys = ['a', 'b', 'c']

values = [1, 2, 3]

my_dict = dict(zip(keys, values))

for key, value in my_dict.items():

   print(key, value)
You can also try this code with Online Python Compiler
Run Code


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:

  • Python

Python

my_dict = {'a': 1, 'b': 2, 'c': 3}

a, b, c = my_dict

print(a)

print(b)

print(c)
You can also try this code with Online Python Compiler
Run Code


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.

Frequently Asked Questions

Can I modify a dictionary while iterating over it?

It's not recommended to modify a dictionary while iterating over it, as it can lead to unpredictable behavior. Instead, create a new dictionary or use a list to store the changes.

What happens if I try to access a key that doesn't exist in the dictionary?

If you try to access a key that doesn't exist in the dictionary using the square bracket notation (my_dict[key]), it will raise a KeyError. To avoid this, you can use the get() method, which returns None or a default value if the key is not found.

Can I iterate over a dictionary in a specific order?

Dictionaries in Python are unordered by default. However, you can use the sorted() function to sort the keys or values before iterating over them. Alternatively, you can use an OrderedDict from the collections module to maintain the insertion order of key-value pairs.

Conclusion

In this article, we learned various ways to iterate over a dictionary in Python. We explained different methods like keys(), values(), and items() to access keys, values, or key-value pairs respectively. We also saw how to use a for loop directly on a dictionary, use map() with dict.get, zip() keys and values, and unpack a dictionary. 

You can also check out our other blogs on Code360.

Live masterclass