Python Dictionary items() Method
The dict.items() method lets you get both the keys and values from a dictionary at the same time.
Beginners usually use it when looping through a dictionary. Instead of getting a key first and then looking up its value, items() gives you each key-value pair together.
Quick example
person = {"name": "Ana", "age": 25}
for key, value in person.items():
print(key, value)
Output:
name Ana
age 25
Use items() when you need both the dictionary key and its value in the same loop.
What items() does
items() returns all key-value pairs from a dictionary.
Important points:
- Each result is a pair in the form
(key, value) - Each pair is a tuple
- The most common use is looping through a dictionary
Example:
person = {"name": "Ana", "age": 25}
print(list(person.items()))
Output:
[('name', 'Ana'), ('age', 25)]
If you are still learning dictionaries, see Python dictionaries explained or what is a dictionary in Python.
Basic syntax
The syntax is simple:
my_dict.items()
Things to remember:
items()does not take any arguments- It does not change the original dictionary
- It gives you a way to access key-value pairs
Example:
scores = {"Alice": 90, "Bob": 85}
result = scores.items()
print(result)
print(scores)
Output:
dict_items([('Alice', 90), ('Bob', 85)])
{'Alice': 90, 'Bob': 85}
The dictionary stays the same.
What it returns
items() returns a dict_items view object.
That means:
- You can loop over it
- You can convert it to a list if needed
- It reflects changes made to the dictionary
Example:
data = {"x": 1, "y": 2}
pairs = data.items()
print(list(pairs))
data["z"] = 3
print(list(pairs))
Output:
[('x', 1), ('y', 2)]
[('x', 1), ('y', 2), ('z', 3)]
This happens because pairs is a view of the dictionary data, not a separate copied list.
Looping with items()
This is the most common way beginners use items():
student = {"name": "Mia", "grade": "A", "age": 14}
for key, value in student.items():
print(f"{key}: {value}")
Output:
name: Mia
grade: A
age: 14
This pattern is clearer than looping through keys and then looking up each value:
student = {"name": "Mia", "grade": "A", "age": 14}
for key in student:
print(f"{key}: {student[key]}")
Both work, but for key, value in student.items() is often easier to read when you need both parts.
For a full guide, see how to loop through a dictionary in Python.
Converting the result
If you want to see all pairs clearly, you can convert the result to a list:
colors = {"apple": "red", "banana": "yellow", "grape": "purple"}
pairs_list = list(colors.items())
print(pairs_list)
Output:
[('apple', 'red'), ('banana', 'yellow'), ('grape', 'purple')]
This creates a list of tuples.
Use this when:
- You want clearer printed output
- You need list behavior
- You want to inspect all pairs at once
items() vs keys() vs values()
Dictionary methods give different parts of the dictionary:
items()gives both key and valuekeys()gives only keysvalues()gives only values
Example:
person = {"name": "Ana", "age": 25}
print(list(person.keys()))
print(list(person.values()))
print(list(person.items()))
Output:
['name', 'age']
['Ana', 25]
[('name', 'Ana'), ('age', 25)]
Use the method that matches your task:
When to use items()
items() is useful for tasks like:
- Printing dictionary contents
- Building formatted output
- Looping through settings and values
- Showing names with scores
- Working with labels and their data
Example:
settings = {
"theme": "dark",
"language": "English",
"notifications": True
}
for setting, value in settings.items():
print(f"{setting} = {value}")
Output:
theme = dark
language = English
notifications = True
Common mistakes
Here are some common beginner mistakes when using items().
Trying to unpack items() incorrectly
Each result from items() has two parts: a key and a value.
This works:
person = {"name": "Ana", "age": 25}
for key, value in person.items():
print(key, value)
This is wrong because it tries to unpack into only one variable pattern:
person = {"name": "Ana", "age": 25}
for key in person.items():
print(key)
This code is not an error, but key is actually the full tuple, such as ('name', 'Ana'), not just the key.
If you want the full pair, use one variable:
for item in person.items():
print(item)
If you want separate key and value, use two variables:
for key, value in person.items():
print(key, value)
Expecting items() to return a regular list
items() returns a dict_items object, not a list.
Check it:
my_dict = {"a": 1, "b": 2}
print(my_dict.items())
print(type(my_dict.items()))
print(list(my_dict.items()))
Output:
dict_items([('a', 1), ('b', 2)])
<class 'dict_items'>
[('a', 1), ('b', 2)]
Changing the dictionary while looping over items()
Be careful when changing a dictionary during iteration.
Problem example:
numbers = {"a": 1, "b": 2, "c": 3}
for key, value in numbers.items():
if value == 2:
numbers["d"] = 4
Changing the dictionary size while looping can cause errors.
A safer approach is to loop over a copied list of items:
numbers = {"a": 1, "b": 2, "c": 3}
for key, value in list(numbers.items()):
if value == 2:
numbers["d"] = 4
print(numbers)
Forgetting that each result is a key-value pair
If you print each item directly, you will see tuples:
data = {"x": 10, "y": 20}
for item in data.items():
print(item)
Output:
('x', 10)
('y', 20)
That is normal. Each item is one (key, value) pair.
FAQ
What does dictionary items() return in Python?
It returns a dict_items view containing key-value pairs as tuples.
Does items() return a list?
No. It returns a view object. Use list(my_dict.items()) if you need a list.
Can I loop through keys and values with items()?
Yes. Use this pattern:
for key, value in my_dict.items():
print(key, value)
Does items() change the dictionary?
No. It only gives you a way to access the pairs.