items() returns a view of the dict's entries as (key, value) tuples, printed as dict_items([...]). It is the standard way to loop over keys and values at the same time.
The view is live, and when every value is hashable it is also set-like, so you can subtract two items views to find entries that differ. Feeding it to sorted() is how you sort a dictionary.
dict.items() is an instance method of the built-in dict type: call it on a dictionary with dot notation, such as grades.items(...). No import is needed.
Syntax
Unpack each pair straight into two loop variables.
python
for key, value in d.items():
print(key, value)
pairs = list(d.items())
Call
What it does
{"a": 1, "b": 2}.items()
Returns dict_items([('a', 1), ('b', 2)]).
list({"a": 1}.items())
Returns [('a', 1)].
("a", 1) in {"a": 1}.items()
Returns True: the key exists and its value matches.
Membership tests on an items view check the key and the value together, so ("a", 2) in {"a": 1}.items() is False.
Method signatures
python
dict.items()
# returns: a dict_items view of (key, value) tuples
Parameters
items() takes no parameters.
Return Value
Return Value
Description
dict_items
A live view of (key, value) tuples in insertion order. It supports len(), in, iteration, reversed() (Python 3.8+), and set operators when the values are hashable.
Python Examples
Example 1 — Print a Grade Report from Name/Score Pairs
Unpack each pair in the loop header and format a small aligned report.
python
grades = {"Nina": 91, "Omar": 78, "Priya": 85}
for name, score in grades.items():
status = "pass" if score >= 80 else "review"
print(f"{name:<6}{score:>4} {status}")
print(grades.items())
print(len(grades.items()), ("Omar", 78) in grades.items())