A Python dictionary (dict) stores key-value pairs and owns the data. A dictionary view is a lightweight object returned by dict.keys(), dict.values() or dict.items() that gives a live, read-only window onto that dictionary. The view holds no copy, so it always reflects later changes to the dictionary.
Since Python 3.7, dictionaries keep insertion order, and views iterate in that same order. This article explains how the two differ, what views can do that lists cannot, and the mistakes to avoid.
A dictionary is a mutable mapping. Keys must be hashable (strings, numbers, tuples of hashables), values can be anything, and lookups by key take O(1) time on average.
Older tutorials call dictionaries unordered. That was true before Python 3.7. Since then the language guarantees that a dictionary remembers insertion order, although two dictionaries with the same items in a different order still compare equal.
The three view methods return dict_keys, dict_values and dict_items objects:
| Method | View type | Contains |
|---|---|---|
d.keys() | dict_keys | Keys |
d.values() | dict_values | Values |
d.items() | dict_items | (key, value) tuples |
A view is not a snapshot. It is a window, so it changes when the dictionary changes:
In Python 2, keys() returned a list copy. In Python 3 it returns a view, which is why code written for Python 2 sometimes breaks, for example when it indexes d.keys()[0].
Views support iteration, len(), membership tests and reversed() (Python 3.8 and later). They do not support indexing or slicing.
keys() views, and items() views whose values are hashable, behave like sets. This gives you short, readable comparisons:
values() views are not set-like, because values can repeat and may be unhashable.
Use a view when you only need to read or loop. Use a list when you need a stable copy:
Changing the value of an existing key during iteration is allowed. Adding or removing keys is what raises RuntimeError.
Because views are read-only, you cannot assign through them. To change the dictionary, use the dictionary itself, for example d[key] = value or d.update(other).
Creating a view is O(1) and uses a small fixed amount of memory, no matter how large the dictionary is. Converting it with list(d.items()) is O(n) in time and memory. Prefer iterating the view directly in loops and pass views to functions such as sum(d.values()), max(d.values()) or sorted(d.items()) without converting first.
If you need to hand out a read-only dictionary rather than a view, wrap it with types.MappingProxyType(d).
No. It is a live window onto the dictionary. Later additions, removals and updates are visible through the view. Convert it with list() or dict() if you need a snapshot.
d.keys()?Views are not sequences. Use list(d)[0] for a one-off, or next(iter(d)) to get the first key without building a list.
Yes, since Python 3.7 they keep insertion order as part of the language. Equality between dictionaries ignores order, though.
.keys() to loop over a dictionary?No. for key in d: already iterates over the keys, and key in d already tests keys. Use .keys() mainly for set operations or when it makes the code clearer.
A dictionary stores the data, and a view is a cheap, live, read-only window onto its keys, values or items. Views support iteration, length, membership and (for keys and items) set operations, but not indexing. Do not add or remove keys while iterating a view; iterate a copy or build a new dictionary instead.