Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › Python › Reference › items()

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.

Input:

                
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
1

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.

2

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)]
3

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()).

4

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}
5

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) pairsYou need both
keysKeys onlyYou need only keys
valuesValues onlyYou need only values

⚠️ Common Mistakes

❓ 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.

🎯 What's Next?

← All Python Reference