Python Dictionary items() Method

Short answer
dict.items() returns a live view of the dictionary's (key, value) pairs, most often used as for key, value in d.items(): to loop over keys and values together.

When you need both halves of every entry, items() is the method to reach for. Each pair arrives as a two-item tuple, which Python unpacks into two loop variables for you. This is the standard way to loop through a dictionary and a key part of the dictionary methods toolkit.

The result is not a list but a view object. It stays connected to the dictionary, takes almost no memory and always reflects the current contents.

Quick facts
Syntaxmy_dict.items()
ParametersNone
ReturnsA dict_items view of (key, value) tuples
OrderInsertion order (guaranteed since Python 3.7)
Time complexityO(1) to create the view; O(n) to iterate

How items() and tuple unpacking work together

Each element of the view is a tuple such as ("pen", 3). Writing for name, price in prices.items() unpacks that tuple into name and price on every pass. Without unpacking you would get the whole tuple in a single variable.

Python
prices = {"pen": 3, "book": 12}
print(prices.items())
for name, price in prices.items():
    print(name, price)

The first line shows the view's printed form, dict_items([...]).

items() is a live view, not a copy

A view reflects later changes. Store view = d.items(), add a key to d, and view shows it. That is efficient, but it also means you must not add or remove keys while looping over the view: Python raises RuntimeError: dictionary changed size during iteration. To change the dictionary while looping, loop over a snapshot with list(d.items()).

Views also support len(), membership tests with in (for example ("pen", 3) in prices.items()) and set-like operations such as & and -, because the pairs are hashable as long as the values are. When you need indexing or sorting, convert with list() or sorted(), as in sorting a dictionary by value.

items() compared with keys() and values()

Three views of the same dictionary.
MethodYieldsTypical use
items()(key, value) pairsLoop when you need both, build a new dict
keys()Keys onlyCheck or compare the key set
values()Values onlySum, max or min of the values
for k in dKeys onlyShortcut for looping over keys()

Examples you can run

Press Run, then edit the code and run it again.

Print a receipt from a dictionary

Unpack each pair into item and price, and add up the prices as you go.

Print a receipt from a dictionary
cart = {"pen": 3, "notebook": 45, "ruler": 10}
total = 0
for item, price in cart.items():
    print(f"{item}: {price}")
    total += price
print("Total:", total)
Expected outputpen: 3
notebook: 45
ruler: 10
Total: 58

Filter a dictionary with a comprehension

items() feeds a dictionary comprehension that keeps only the passing scores.

Filter a dictionary with a comprehension
scores = {"asha": 82, "ravi": 45, "meera": 91, "john": 38}
passed = {name: s for name, s in scores.items() if s >= 50}
print(passed)
Expected output{'asha': 82, 'meera': 91}

The view updates when the dictionary changes

The same view object shows the new entry without calling items() again.

The view updates when the dictionary changes
stock = {"apples": 4}
view = stock.items()
stock["pears"] = 7
print(list(view))
print(len(view))
Expected output[('apples', 4), ('pears', 7)]
2

Common mistakes

Forgetting to unpack the pairs

✗ Wrong

ages = {"asha": 21}
for pair in ages:
    print(pair)
Prints only 'asha': looping over a dictionary gives keys, not pairs

✓ Right

ages = {"asha": 21}
for name, age in ages.items():
    print(name, age)

Loop over ages.items() and unpack into two variables to get both key and value.

Deleting keys while looping over items()

✗ Wrong

d = {"a": 1, "b": 2}
for k, v in d.items():
    if v == 1:
        del d[k]
RuntimeError: dictionary changed size during iteration

✓ Right

d = {"a": 1, "b": 2}
for k, v in list(d.items()):
    if v == 1:
        del d[k]
print(d)

Loop over a copy of the pairs with list(d.items()), or build a new dictionary with a comprehension.

Indexing the view like a list

✗ Wrong

d = {"a": 1}
print(d.items()[0])
TypeError: 'dict_items' object is not subscriptable

✓ Right

d = {"a": 1}
print(list(d.items())[0])

Convert to a list first: list(d.items())[0].

Python Dictionary items() Method: questions people ask

What does items() return in Python?

It returns a dict_items view of the dictionary's (key, value) tuples. The view is live, so it reflects later changes to the dictionary.

How do I loop through a dictionary's keys and values together?

Use for key, value in my_dict.items():. Each pair is unpacked into the two variables.

How do I convert dict.items() to a list?

Wrap it in list(): list(my_dict.items()) gives a list of (key, value) tuples. Use dict() on that list to turn the pairs back into a dictionary.

Keep learning

Step-by-step lessons:
Python DictionariesPython Dictionary MethodsPython for Loops

Related:
How to Loop Through a Dictionary in PythonPython Dictionary keys() MethodPython Dictionary values() MethodHow to Sort a Dictionary by Value in Python

More in dictionary methods:
Python Dictionary get() MethodPython Dictionary keys() MethodPython Dictionary values() MethodPython Dictionary update() MethodPython Dictionary pop() MethodPython Dictionary popitem() MethodPython Dictionary setdefault() MethodPython Dictionary clear() Method See all →