Python Reference
dict.items()
Return (key, value) pairs from a dictionary. Ideal for loops.
Last updated: October 2026
Definition: dict.items() returns a view object containing the dictionary's (key, value) pairs as tuples. It is dynamic, iterable, and supports set operations.
🎬 Live Example
Five common items patterns. Click any button to see the output.
💡 items() is the Pythonic way to iterate key-value pairs together.
Basic Syntax
dict.items()
# No arguments
# Returns: dict_items view of (key, value) tuples
# Dynamic — reflects dict changes
# Iterable and set-like
Basic Iteration with Unpacking
d = {"name": "Alice", "age": 30}
for key, value in d.items():
print(f"{key}: {value}")
# name: Alice
# age: 30
Best practice: Always use for k, v in d.items() when you need both key and value.
View Object (Not a List)
d = {"a": 1, "b": 2}
d.items()
# dict_items([('a', 1), ('b', 2)])
# Convert to list if needed
list(d.items())
# [('a', 1), ('b', 2)]
Dynamic View
d = {"a": 1}
items = d.items()
d["b"] = 2
list(items)
# [('a', 1), ('b', 2)] ← reflects new pair
del d["a"]
list(items)
# [('b', 2)]
Note: Don't modify the dict while iterating items(). Convert to a list first: list(d.items()).
Shallow Copy Pattern
original = {"a": 1, "b": 2}
# Create a copy
copy = dict(original.items())
# or simply: copy = dict(original)
copy["c"] = 3
# original: {'a': 1, 'b': 2}
# copy: {'a': 1, 'b': 2, 'c': 3}
Real-World: Sort by Value
scores = {"Alice": 85, "Bob": 92, "Cara": 78}
# Sort by value (descending)
sorted_items = sorted(scores.items(), key=lambda x: x[1], reverse=True)
# [('Bob', 92), ('Alice', 85), ('Cara', 78)]
# Find highest scorer
top = max(scores.items(), key=lambda x: x[1])
# ('Bob', 92)
🎯 items vs keys vs values
| Method | Returns | Use when |
|---|---|---|
items | (key, value) pairs | You need both |
keys | Keys only | You need only keys |
values | Values only | You need only values |
⚠️ Common Mistakes
- Modifying dict while iterating items(). Convert to list first.
- Treating items() as a list. It's a view — wrap in list() if needed.
- Using items() when you only need keys. Iterate the dict directly.
- Forgetting tuple unpacking.
for pair in d.items()gives tuples, not values. - Assuming order before Python 3.7. Dicts are ordered now, but not in older versions.
❓ Frequently Asked Questions
What does dict.items() do in Python?
dict.items() returns a view object containing the dictionary's (key, value) pairs as tuples. It is dynamic, iterable, and reflects changes made to the dict.
How do I iterate over a dict with items()?
Use tuple unpacking: for key, value in d.items(). This gives you both the key and the value in each iteration, which is more readable than fetching the value separately.
Is dict.items() a list of tuples?
No. In Python 3 it returns a view object, not a list. Wrap in list() if you need a list: list(d.items()).
Can I convert items() to a dictionary?
Yes. Use dict(d.items()) to create a shallow copy of the dictionary. This is useful when you want a snapshot of the current state.
Does items() reflect changes to the dict?
Yes. The items view is dynamic. Adding or removing keys updates the view automatically.