Post

Python Dictionary items()

The items() method returns a view object of the dictionary. A view object is the list containing the key-value pairs of the dictionary as tuples inside the list.

Python Dictionary items()

When you work with Python dictionaries, there are many scenarios where you need to access both the key and its associated value at the same time. Rather than looping through keys and looking up each value separately, Python provides the items() method to give you both at once. This method is one of the most frequently used dictionary methods in Python, and understanding it well can make your code cleaner, more efficient, and more Pythonic. In this post, we will cover the items() method in full detail — syntax, parameters, examples, real-world use cases, common mistakes, tips, and a FAQ section.

What is the items() Method?

The items() method returns a view object that contains all the key-value pairs of the dictionary as tuples. Each tuple holds a key and its corresponding value. Like all dictionary view objects in Python 3, the view returned by items() is dynamic — it reflects changes to the dictionary in real time.

Syntax

The syntax of items() is:

1
dictionary.items()

items() Parameters

ParameterDescription
NoneThe items() method does not take any parameters as arguments.

No arguments are needed. Simply call the method on any dictionary object.

Return Value

The method returns a dict_items view object, which contains all key-value pairs as a sequence of (key, value) tuples. This view is live — modifications to the dictionary are automatically reflected in the view.


Example 1: Basic Usage of items()

1
2
3
cars = { 1 : "BMW", 2 : "TOYOTA", 3 : "TATA"}

print(cars.items())

Output:

1
dict_items([(1, 'BMW'), (2, 'TOYOTA'), (3, 'TATA')])

The output shows each key-value pair as a tuple inside the dict_items view. The integer keys map to their respective car brand strings.


Example 2: Iterating Over items() with a for Loop

The most common use of items() is to unpack key-value pairs during iteration, making loops very readable.

1
2
3
4
5
6
7
8
9
employee = {
    "name": "John",
    "department": "Engineering",
    "salary": 75000,
    "location": "New York"
}

for key, value in employee.items():
    print(f"{key}: {value}")

Output:

1
2
3
4
name: John
department: Engineering
salary: 75000
location: New York

By unpacking each tuple into key and value variables directly in the for statement, you avoid the need for dict[key] lookups inside the loop.


Example 3: Converting items() to a List

You can convert the dict_items view to a list of tuples when you need static access or want to use list operations.

1
2
3
4
5
6
product = {"id": 101, "name": "Laptop", "price": 999.99, "stock": 50}

items_list = list(product.items())
print(items_list)
print("First item:", items_list[0])
print("Last item:", items_list[-1])

Output:

1
2
3
[('id', 101), ('name', 'Laptop'), ('price', 999.99), ('stock', 50)]
First item: ('id', 101)
Last item: ('stock', 50)

Converting to a list lets you use index access, slicing, and other list-specific operations that are not available on the raw view object.


Example 4: Dynamic View — Reflecting Dictionary Changes

Like keys() and values(), the items() view dynamically tracks changes to the dictionary.

1
2
3
4
5
6
7
8
9
config = {"debug": True, "version": "1.0"}
view = config.items()

print("Before:", view)

config["author"] = "Alice"
config["debug"] = False

print("After:", view)

Output:

1
2
Before: dict_items([('debug', True), ('version', '1.0')])
After: dict_items([('debug', False), ('version', '1.0'), ('author', 'Alice')])

The view automatically reflects both the updated value for debug and the newly added author key.


Real-World Use Cases

1. Building Formatted Output or Reports

When generating configuration summaries, reports, or logs, items() lets you cleanly format key-value data.

1
2
3
4
5
6
7
8
9
10
server_config = {
    "host": "192.168.1.1",
    "port": 8080,
    "timeout": 30,
    "ssl": True
}

print("=== Server Configuration ===")
for key, value in server_config.items():
    print(f"  {key.upper()}: {value}")

Output:

1
2
3
4
5
=== Server Configuration ===
  HOST: 192.168.1.1
  PORT: 8080
  TIMEOUT: 30
  SSL: True

2. Filtering Key-Value Pairs

You can use items() with a list comprehension to filter only the pairs that match a condition.

1
2
3
4
inventory = {"apples": 0, "bananas": 15, "oranges": 0, "grapes": 8}

in_stock = {k: v for k, v in inventory.items() if v > 0}
print("In stock:", in_stock)

Output:

1
In stock: {'bananas': 15, 'grapes': 8}

This pattern is a clean and Pythonic way to filter dictionaries based on value conditions.

3. Merging and Transforming Dictionaries

When transforming data — for example, converting all string values to uppercase or multiplying numeric values — items() is the ideal tool.

1
2
3
4
prices = {"apple": 1.5, "banana": 0.75, "cherry": 3.0}

discounted = {item: round(price * 0.9, 2) for item, price in prices.items()}
print("Discounted prices:", discounted)

Output:

1
Discounted prices: {'apple': 1.35, 'banana': 0.68, 'cherry': 2.7}

Common Mistakes

Mistake 1: Using Index Access on dict_items Directly

The dict_items view does not support integer indexing. Attempting to do so raises a TypeError.

1
2
3
4
5
6
7
8
9
d = {"a": 1, "b": 2}
items = d.items()

# Wrong — raises TypeError
# print(items[0])

# Correct
items_list = list(d.items())
print(items_list[0])  # ('a', 1)

Mistake 2: Expecting a Static Snapshot

If you store the items() view and then modify the dictionary, the view will change. If you need a fixed copy, convert to a list immediately.

1
2
3
4
5
6
7
d = {"x": 10}
items = d.items()  # This is a live view
d["y"] = 20
print(items)  # dict_items([('x', 10), ('y', 20)]) — changed!

# For a frozen copy:
items_snapshot = list(d.items())

Mistake 3: Modifying the Dictionary During Iteration

Changing the size of the dictionary while iterating over items() raises a RuntimeError: dictionary changed size during iteration.

1
2
3
4
5
6
7
8
9
10
11
12
d = {"a": 1, "b": 2, "c": 3}

# BAD — raises RuntimeError
# for k, v in d.items():
#     if v == 2:
#         del d[k]

# SAFE — iterate over a list copy
for k, v in list(d.items()):
    if v == 2:
        del d[k]
print(d)  # {'a': 1, 'c': 3}

Tips and Best Practices

  • Always unpack tuples in the for loop (for k, v in d.items()) rather than accessing item[0] and item[1] — it is cleaner and more readable.
  • Use items() in dictionary comprehensions for filtering or transforming dictionaries efficiently.
  • Convert to list() when you need index access or a static snapshot.
  • Use items() instead of looping over keys() and doing d[key] lookups — it is more efficient and Pythonic.
  • When comparing two dictionaries, items() can be converted to sets for efficient intersection or difference operations.

FAQ

Q1: What is the difference between keys(), values(), and items()?

keys() returns a view of all keys. values() returns a view of all values. items() returns a view of all key-value pairs as tuples. Use keys() when you only need to work with keys, values() when you only need values, and items() when you need both keys and values together.

Q2: Can I use items() to compare two dictionaries?

Yes. You can convert items() to a set and perform set operations to find common pairs, differences, or additions between two dictionaries.

1
2
3
4
5
d1 = {"a": 1, "b": 2}
d2 = {"b": 2, "c": 3}

common = set(d1.items()) & set(d2.items())
print("Common items:", common)  # {('b', 2)}

Q3: Does items() preserve insertion order?

Yes, in Python 3.7 and later, dictionaries maintain insertion order. The items() view returns pairs in the same order they were inserted into the dictionary. In Python 3.6, this behavior is an implementation detail of CPython but not guaranteed by the language specification.


Conclusion

The items() method is one of the most versatile and commonly used methods in Python’s dictionary toolkit. It gives you simultaneous access to both keys and values, supports dynamic views, and enables elegant patterns like comprehensions and formatted output generation. Whether you are filtering data, building reports, or transforming dictionaries, items() is the method to reach for. Mastering it will significantly improve your Python code quality and readability.

Khushal Jethava
Khushal Jethava

Machine Learning Engineer at Codiste, specializing in Generative AI, NLP, and Computer Vision. Building production AI systems with Python.

This post is licensed under CC BY 4.0 by the author.