dict.keys() / values() / items()
Gets dynamic views of dict keys, values, or (key, value) pairs.
data.keys()
data.values()
data.items()This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Gets dynamic views of dict keys, values, or (key, value) pairs.
In Python 3, dict.keys(), dict.values(), and dict.items() return dynamic view objects (eys, alues, and tems). Unlike Python 2's list-returning methods, these views provide a live reflection of the dictionary's state; any modifications to the dictionary are immediately visible in the view. Performance is highly optimized: membership testing using the 'in' operator on a 'keys' view is O(1) because it leverages the underlying hash table. The 'keys' and 'items' views also function like sets, supporting set-theoretic operations such as intersection (&) and union (|) if the dictionary values are hashable. These objects are extremely memory-efficient as they do not duplicate the dictionary's contents. Furthermore, since Python 3.7, these views are guaranteed to maintain the insertion order of the keys.
Quick reference
Syntax
data.keys()
data.values()
data.items()
See it in practice
Examples
Iterating with Key-Value Unpacking
inventory = {'apples': 5, 'oranges': 10}
for fruit, count in inventory.items():
print(f'{fruit}: {count}')apples: 5 oranges: 10
The .items() method returns a view of tuples, allowing for clean sequence unpacking directly in the loop header.
Dynamic View Behavior
prices = {'cpu': 300}
view = prices.keys()
prices['gpu'] = 500
print('gpu' in view)
print(list(view))True ['cpu', 'gpu']
Changes to the dictionary automatically update existing view objects without requiring a new method call.
Set Operations on Dictionary Keys
admin_perms = {'read', 'write', 'delete'}
user_perms = {'id': 1, 'read': True, 'write': True}
common = admin_perms & user_perms.keys()
print(common){'read', 'write'}
Dictionary keys views support set operations, making it trivial to find intersections or differences between keys and other sets or dictionaries.
Debug faster
Common Errors
TypeError
Cause: Attempting to access a view object by index (e.g., keys()[0]). View objects are iterable but do not support indexing or slicing.
Fix: Convert the view object to a list using the list() constructor if numerical indexing is required.
data = {'a': 1}
# Bad: data.keys()[0]
# Good:
key_list = list(data.keys())
print(key_list[0])RuntimeError
Cause: Modifying the size of the dictionary (adding or removing items) while iterating over a view object.
Fix: Iterate over a static copy of the keys by wrapping the view in list() if you need to delete items during the loop.
d = {'a': 1, 'b': 2}
# Raises RuntimeError:
# for k in d.keys(): del d[k]
# Correct:
for k in list(d.keys()):
del d[k]Runtime support
Compatibility
Views exist since Python 3, dynamic and lightweight.
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Gets dynamic views of dict keys, values, or (key, value) pairs.
TypeError: Convert the view object to a list using the list() constructor if numerical indexing is required. RuntimeError: Iterate over a static copy of the keys by wrapping the view in list() if you need to delete items during the loop.