Python dict.items() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
dict method

Definition

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())
CallWhat 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 ValueDescription
dict_itemsA 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())

How It Works

  1. Two names per pair. for name, score in ... unpacks each tuple, so no indexing like pair[0] is needed.
  2. Aligned columns. :<6 pads the name on the right and :>4 pads the score on the left.
  3. Pairs are tuples. The view's repr shows each entry as a 2-tuple, in insertion order.
  4. Pair membership. ("Omar", 78) in grades.items() is True because both the key and its value match.

Example 2 — Sort App Downloads by Size and by Name

Sort the pairs by value with a key function, rebuild a dict from the result, and take the two largest.

python
downloads = {"maps": 85.5, "chat": 142.0, "camera": 12.25, "music": 120.75}

smallest_first = sorted(downloads.items(), key=lambda pair: pair[1])
print(smallest_first[0])
print(dict(smallest_first))
print(dict(sorted(downloads.items())))
largest = sorted(downloads.items(), key=lambda pair: pair[1], reverse=True)
print([name for name, size in largest[:2]])

How It Works

  1. Key function. lambda pair: pair[1] tells sorted() to compare the sizes, giving a list of tuples from smallest to largest.
  2. Back to a dict. dict() accepts the sorted pairs, and because dicts keep insertion order the new dict stays sorted by size.
  3. Default sort. Without a key, tuples compare by their first item, so the pairs come out in alphabetical order of app name.
  4. Top two. reverse=True puts the biggest first and slicing [:2] keeps chat and music.

Example 3 — Invert Country Codes and Diff Two Versions

Swap keys and values with a comprehension, watch the live view grow, then subtract an older version of the data.

python
codes = {"IN": "India", "JP": "Japan", "BR": "Brazil"}
pairs = codes.items()

names_to_codes = {name: code for code, name in pairs}
print(names_to_codes)

codes["KE"] = "Kenya"
print(len(pairs), ("KE", "Kenya") in pairs)

old = {"IN": "India", "JP": "Nippon"}
print(sorted(pairs - old.items()))

How It Works

  1. Swap in a comprehension. Unpacking code, name and writing name: code builds the reverse lookup. This only works cleanly when the values are unique.
  2. Still live. Adding Kenya after pairs was created raises its length to 4 and makes the new pair a member.
  3. Diff by pair. Subtracting the old items view keeps pairs that are new or changed: Brazil and Kenya are new, and Japan's value changed.
  4. Stable printout. The subtraction returns a set of tuples, so sorted() orders them by country code before printing.

Frequently Asked Questions

A dict_items view of (key, value) tuples in insertion order. It reflects later changes to the dict; use list(d.items()) for a fixed list.
Use for key, value in d.items():. Each pair is unpacked into the two loop variables.
Use sorted(d.items(), key=lambda pair: pair[1]) for a list of pairs, and wrap it in dict() to get a dict in that order.
You can assign new values to existing keys. Adding or removing keys raises RuntimeError, so loop over list(d.items()) in that case.
Use next(iter(d.items())). It raises StopIteration on an empty dict, so check the dict first or pass a default to next().

Next: dict.keys()

Return a live, set-like view of the keys of a dict.

keys() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful