dict.items()
dict.items() dict_items · Updated March 13, 2026 · Dict Methods The .items() method returns a view object that displays a list of dictionary’s key-value pairs as tuples. This view provides a dynamic view of the dictionary’s items, meaning it reflects changes to the dictionary.
Syntax
dict.items()
Parameters
None. This method takes no parameters.
Return Value
Returns a dict_items view object, which yields key-value tuple pairs on iteration. The view is dynamic—if the dictionary changes, the view reflects those changes.
Basic Examples
Iterating over key-value pairs
The most common use case is iterating over a dictionary:
person = {"name": "Alice", "age": 30, "city": "New York"}
for key, value in person.items():
print(f"{key}: {value}")
# name: Alice
# age: 30
# city: New York
Converting to a list
If you need an actual list instead of a view:
scores = {"alice": 95, "bob": 87, "charlie": 92}
items_list = list(scores.items())
print(items_list)
# [('alice', 95), ('bob', 87), ('charlie', 92)]
Checking if a key-value pair exists
config = {"debug": True, "port": 8080}
if ("debug", True) in config.items():
print("Debug mode is enabled")
# Debug mode is enabled
How Views Work
A key distinction: .items() returns a view, not a copy. Changes to the dictionary are reflected in the view:
d = {"a": 1, "b": 2}
items_view = d.items()
# Add a new item to the dictionary
d["c"] = 3
# The view now shows the new item!
print(list(items_view))
# [('a', 1), ('b', 2), ('c', 3)]
If you need a snapshot that won’t change, convert to a list first:
d = {"a": 1}
snapshot = list(d.items())
d["b"] = 2
# Snapshot unchanged
print(snapshot)
# [('a', 1)]
Common Patterns
Dictionary comprehension from two lists
keys = ["name", "age", "city"]
values = ["Alice", 30, "New York"]
data = dict(zip(keys, values))
print(data)
# {'name': 'Alice', 'age': 30, 'city': 'New York'}
Filtering a dictionary
prices = {"apple": 1.5, "banana": 0.5, "cherry": 3.0, "date": 2.0}
# Keep only items with value > 1.0
expensive = {k: v for k, v in prices.items() if v > 1.0}
print(expensive)
# {'apple': 1.5, 'cherry': 3.0}
Inverting a dictionary
original = {"a": 1, "b": 2, "c": 3}
inverted = {v: k for k, v in original.items()}
print(inverted)
# {1: 'a', 2: 'b', 3: 'c'}
Merging dictionaries
defaults = {"theme": "dark", "language": "en"}
user_overrides = {"theme": "light"}
# Merge with user overrides taking precedence
merged = {**dict(defaults.items()), **dict(user_overrides.items())}
print(merged)
# {'theme': 'light', 'language': 'en'}
Unpacking in function calls
config = {"host": "localhost", "port": 8080}
def connect(host, port):
print(f"Connecting to {host}:{port}")
connect(**dict(config.items()))
# Connecting to localhost:8080
items() vs keys() vs values()
| Method | Returns | Example |
|---|---|---|
.keys() | View of keys | ['name', 'age'] |
.values() | View of values | ['Alice', 30] |
.items() | View of (key, value) tuples | [('name', 'Alice'), ('age', 30)] |
Performance Note
The .items() view is memory-efficient because it doesn’t create a copy of the data. For large dictionaries, iterating over .items() is typically faster than iterating over keys and looking up values:
# Slower - two lookups per iteration
for key in d:
value = d[key]
# Faster - tuple unpacking
for key, value in d.items():
# use both directly
See Also
- dict() — the dict constructor for creating new dictionaries
- collections module — contains OrderedDict and Counter for specialized dictionary operations
- dict.get() — safely retrieve values from a dictionary