How to Loop Through a Dictionary in Python
If you want to go through the contents of a dictionary in Python, there are three main patterns to know:
- loop through keys
- loop through values
- loop through keys and values together
This page shows the most useful ways to do that with simple examples.
Quick answer
student = {"name": "Ana", "age": 20, "city": "Lima"}
for key, value in student.items():
print(key, value)
Output:
name Ana
age 20
city Lima
Use .items() when you need both the key and the value during the loop.
What this page helps you do
- Loop through dictionary keys
- Loop through dictionary values
- Loop through both keys and values
- Choose the right method for your task
Start with a simple dictionary
A dictionary stores data as key-value pairs.
In this example:
"name"is a key"Ana"is its value
student = {
"name": "Ana",
"age": 20,
"city": "Lima"
}
Expected printed output for this dictionary might look like this, depending on how you loop:
name
age
city
or:
Ana
20
Lima
or:
name Ana
age 20
city Lima
If dictionaries are new to you, see Python dictionaries explained.
Loop through dictionary keys
A basic for loop over a dictionary gives you the keys by default.
student = {
"name": "Ana",
"age": 20,
"city": "Lima"
}
for key in student:
print(key)
Output:
name
age
city
Use this when you only need the keys.
You can also write the same thing with .keys():
for key in student.keys():
print(key)
This works too, but it is often not necessary. A plain loop over the dictionary is shorter and does the same job.
If you want to work directly with dictionary keys, see the dict.keys() method.
Loop through dictionary values
Use .values() when you want only the values.
student = {
"name": "Ana",
"age": 20,
"city": "Lima"
}
for value in student.values():
print(value)
Output:
Ana
20
Lima
This is useful when the keys do not matter.
For more detail, see the dict.values() method.
Loop through keys and values together
Use .items() when you need both parts of each dictionary entry.
student = {
"name": "Ana",
"age": 20,
"city": "Lima"
}
for key, value in student.items():
print(key, value)
Output:
name Ana
age 20
city Lima
This is the most common pattern for printing or processing dictionary contents.
You can format the output more clearly if you want:
for key, value in student.items():
print(f"{key}: {value}")
Output:
name: Ana
age: 20
city: Lima
If you want a method-focused explanation, see the dict.items() method.
When to use each approach
Use the loop style that matches your task:
- Use
for key in data:when you need keys only - Use
for value in data.values():when you need values only - Use
for key, value in data.items():when you need both
Examples:
student = {
"name": "Ana",
"age": 20,
"city": "Lima"
}
# Keys only
for key in student:
print("Key:", key)
# Values only
for value in student.values():
print("Value:", value)
# Keys and values
for key, value in student.items():
print(key, "->", value)
Common beginner mistakes
1. Trying to unpack a plain dictionary loop into key and value
This is wrong:
student = {"name": "Ana", "age": 20}
for key, value in student:
print(key, value)
A plain dictionary loop returns only keys, not key-value pairs.
Use .items() instead:
for key, value in student.items():
print(key, value)
2. Forgetting parentheses in .items() or .values()
These are methods, so you must call them with parentheses.
Wrong:
for value in student.values:
print(value)
Correct:
for value in student.values():
print(value)
3. Changing dictionary size during the loop
Be careful about adding or removing keys while looping.
Problem example:
student = {"name": "Ana", "age": 20, "city": "Lima"}
for key in student:
if key == "age":
del student[key]
This can raise an error because the dictionary size changes during iteration.
A safer approach is to loop over a copy of the keys:
student = {"name": "Ana", "age": 20, "city": "Lima"}
for key in list(student.keys()):
if key == "age":
del student[key]
print(student)
Output:
{'name': 'Ana', 'city': 'Lima'}
Simple variations
Use enumerate(data.items()) if you also need a counter
If you want the position number as well as the key and value, use enumerate().
student = {"name": "Ana", "age": 20, "city": "Lima"}
for index, (key, value) in enumerate(student.items(), start=1):
print(index, key, value)
Output:
1 name Ana
2 age 20
3 city Lima
If this pattern is new to you, see how to use enumerate() in Python.
Use an if statement inside the loop to filter results
You can check conditions inside the loop.
student = {"name": "Ana", "age": 20, "city": "Lima"}
for key, value in student.items():
if key != "city":
print(key, value)
Output:
name Ana
age 20
Loop through nested dictionaries one level at a time
If a value is another dictionary, loop through it separately.
student = {
"name": "Ana",
"grades": {
"math": 90,
"science": 85
}
}
for key, value in student.items():
print("Outer:", key, value)
if isinstance(value, dict):
for inner_key, inner_value in value.items():
print(" Inner:", inner_key, inner_value)
Output:
Outer: name Ana
Outer: grades {'math': 90, 'science': 85}
Inner: math 90
Inner: science 85
Debugging dictionary loop problems
If your loop is not working as expected, these quick checks can help:
print(data)
print(type(data))
print(data.keys())
print(data.values())
print(data.items())
These checks help you confirm:
- that
datais really a dictionary - what keys it contains
- what values it contains
- what key-value pairs Python sees
Common causes of problems include:
- using
for key, value in datainstead ofdata.items() - calling
itemsorvalueswithout parentheses - removing or adding keys during iteration
- confusing dictionary loops with list loops
FAQ
How do I loop through both key and value in a dictionary?
Use:
for key, value in my_dict.items():
print(key, value)
Does looping through a dictionary return keys or values?
By default, looping through a dictionary returns keys.
for key in my_dict:
print(key)
How do I get only dictionary values in a loop?
Use:
for value in my_dict.values():
print(value)
Can I change a dictionary while looping through it?
It is safer not to change its size during the loop. If needed, loop through a copy of the keys first.
for key in list(my_dict.keys()):
# safe place to delete keys if needed
pass