How to iterate Python Dictionary using for loop? You can iterate a dictionary in python over keys, iterate over the key and the value, using the lambda function, etc. In this article, I will explain what is Dictionary? its usage, and how to iterate through for loop with several examples.
1. Quick Examples Iterate Over a Dictionary
If you are in a hurry, below are some quick examples of how to loop through a python dictionary using for loop.
# Quick examples iterate over a dictionary
# Example 1: Loop through by Dict Keys
technology={"Course":"python","Fee":4000,"Duration":"60 days"}
for x in technology:
print(x)
# Example 2: Loop through by Key and Value
technology={"Course":"python","Fee":4000,"Duration":"60 days"}
for key, value in technology.items():
print(key, value)
# Example 3: Use key to get the Value
technology={"Course":"python","Fee":4000,"Duration":"60 days"}
for key in technology:
print(key,technology[key])
# Example 4: Use dict.keys() & dict.values()
technology={"Course":"python","Fee":4000,"Duration":"60 days"}
for key in technology.keys():
print('Key: '+ key)
for value in technology.values():
print('Value: '+value)
# Example 5: Using for loop through dictionary to get index
mydict = {"course": 1,"fee": 2,"duration": 3}
for i in mydict:
print(i,mydict[i])
# Example 6: Using lambda function to iterate over a dictionary
my_squares = {2:4,3:9,4:16,5:25}
my_cubes = list(map(lambda key: key**3, my_squares.keys()))
print(my_cubes)
# Example 7: Iterate dictionary using dictionary comprehension
mydict = {"course": "python","fee": 4000,"duration": "45 days"}
newdict = {key: value for (key, value) in mydict.items()}
print(newdict)
2. What is a Dictionary?
Python dictionary also known as dict is an important data structure that is used to store elements in key-value pairs. Each entry in the dictionary is in the form of key-value pairs. A dictionary is a collection of elements that is unordered, mutable, and does not allow duplicates in keys. Dictionary values should be enclosed with {} and key: value pair separated by commas. The dictionaries are indexed by keys.
Another important feature of dictionaries is that they are mutable data types, so, that you can be added, deleted, and updated their key-value pairs from the dictionary.
Dictionary Key – As I mentioned earlier, Dictionary doesn’t allow duplicated keys and keys must be of immutable type(such as string, number, tuple). in case, you add duplicates, it overrides the existing keys of the dictionary.
Dictionary Value – The values can be of any type of object and can be allowed duplicates. To get a value from a Python Dictionary you have to iterate over keys.
2.1 Create a Dictionary
Let’s create a Python dictionary,
# Create dictionary
mydict = {
"course": "python",
"fee": 4000,
"duration": "60 days"
}
print("Create dictionary:\n",mydict)
Yields below output.

As you see keys of the mydict are course, fee, duration. The values of the mydict are python, 4000, 60 days.
3. Iterate Over Dictionary Keys
Use for loop in order to iterate a python dictionary over keys, by default when you use for loop with dict, it returns a key from the dict for each iteration.
To iterate over dictionary keys in Python, you can use a for loop directly on the dictionary. In this loop, x takes on each key in the dictionary during each iteration. This is a simple and common way to iterate over the keys of a dictionary in Python.
# Loop through by Dict Keys
technology={"Course":"python","Fee":4000,"Duration":"60 days"}
for x in technology:
print(x)
Yields below output. Notice that it just displayed the keys from the dict.
4. Loop Through by Key and Value using dict.items()
Use dict.items() to return a view object that gives a list of dictionary’s (key, value) tuple pairs. so, that You can iterate through tuple to get the dictionary key and value. You can loop through both keys and values using the items() method of a dictionary.
In this loop, key represents the dictionary keys, and value represents the corresponding values. The items() method returns a view of key-value pairs, allowing you to iterate through both components simultaneously. This is a common and efficient way to access both keys and values in a dictionary during iteration.
# Loop through by Key and Value
technology={"Course":"python","Fee":4000,"Duration":"60 days"}
for key, value in technology.items():
print(key, value)
Yields below output
# Output:
Course python
Fee 4000
Duration 60 days
5. Use Key to Get the Value
Use a for loop that iterates over the keys in the technology dictionary. In each iteration, key takes on the value of the current key. Inside the loop, you print both the key and its corresponding value using the print statement. technology[key] is used to access the value associated with the current key.
So, the loop effectively iterates over the keys of the technology dictionary and prints each key along with its associated value. This is a common and concise way to access both keys and values in a dictionary using a for loop.
# Use key to get the Value
technology={"Course":"python","Fee":4000,"Duration":"60 days"}
for key in technology:
print(key,technology[key])
Yields below output.
# Output:
Course python
Fee 4000
Duration 60 days
6. Use dict.keys() & dict.values()
You can also get dict.keys() to iterate over each key, similarly, you can get the dict.values() to iterate each value in python dictionary.
# Use dict.keys() & dict.values()
technology={"Course":"python","Fee":4000,"Duration":"60 days"}
for key in technology.keys():
print('Key: '+ key)
for value in technology.values():
print('Value: '+value)
Yields below output.
# Output:
Key: Course
Key: Fee
Key: Duration
Value: python
Value: 4000
Value: 60 days
7. Iterate Through Dictionary with Index
we can also iterate through the dictionary using the index of the items, which is similar to iterating over the dictionary without using the methods such as keys(), values(), or items().
# Using for loop through dictionary to get index
mydict = {"course": 1,"fee": 2,"duration": 3}
for i in mydict:
print(i,mydict[i])
Yields below output.
# Output:
course 1
fee 2
duration 3
8. Iterate Through Dictionary Using Lambda Function
Python lambda function is an anonymous function that can be used to loop through a dictionary.
8.1 Syntax
# Lambda Syntax
lambda arguments: expression
Using the lambda function along with a Python map() function we can iterate over a dictionary easily.
# Using lambda function to iterate over a dictionary
my_squares = {2:4,3:9,4:16,5:25}
my_cubes = list(map(lambda key: key**3, my_squares.keys()))
print(my_cubes)
#Outputs
[8, 27, 64, 125]
From the above code, we have a lambda function lambda key: key**3 and my_squares.keys() are parameters to the map() function. This will apply the function (key**3) to each key in the dictionary.
9. Iterate over a Python Dictionary using Dictionary Comprehension
Python dictionary comprehension is an elegant and concise way to create dictionaries using iterables. however, it is the same as a list comprehension but the only difference is syntax due to the structure of dictionaries.
9.1 Syntax Of Dictionary Comprehension
# Syntax of the dictionary comprehension
{key: value for variable in iterable(if conditional)}
9.2 Iterate Dictionary Using Dictionary Comprehension
Using dictionary comprehension to iterate through the mydict dictionary and create a new dictionary (newdict) with the same key-value pairs. This is a concise and effective way to create a copy of a dictionary.
# Iterate dictionary using dictionary comprehension
mydict = {"course": "python","fee": 4000,"duration": "45 days"}
newdict = {key: value for (key, value) in mydict.items()}
print(newdict)
# Outputs:
{'course': 'python', 'fee': 4000, 'duration': '45 days'}
In dictionary comprehension, we have iterated through the dictionary using the items() method, which can be accessed both key and value of the Python dictionary.
Frequently Asked Questions on Python Iterate Over A Dictionary
To iterate over only the keys of a dictionary in Python, you can use a for loop directly on the dictionary. In this loop, key takes on each key in the dictionary during each iteration. This is a concise and common way to iterate over the keys of a dictionary in Python.
You can iterate over the values in a dictionary without considering the keys. To do this, you can use the values() method of the dictionary.
To iterate over both keys and values simultaneously in a dictionary, you can use the items() method.
Dictionaries in Python are not ordered, so they do not have index values like lists. However, you can iterate over keys or values using the methods mentioned above, and if you need an ordered collection, you can use an OrderedDict or convert the dictionary to a list of tuples.
In Python 3, there is no practical difference between these two methods. Using for key in my_dict is considered more Pythonic and is often preferred for simplicity and readability.
Conclusion
In this article, I have explained the Python dictionary, how to create it and how to iterate over using for loop. You can iterate through a Python dictionary by keys and by key and value. Also, I have used the lambda function along with map() and used dictionary comprehension to iterate through the dictionary and apply a function to each item in the dictionary.
Happy Learning !!!
Related Articles
- Python Iterate Over an Array
- Python Iterate Over a List
- Python Dictionary Methods
- Access Index in for Loop in Python
- Python Dictionary With Examples
- Python Dictionary Comprehension
- For Loop Break Statement in Python
- For Loop with If Statement in Python
- For Loop Iterate Over an Array in Python
- Python Dictionary keys() Method Usage
- Get Counter Values in a for Loop in Python?
- How to Perform Decrement for Loop in Python