dict.values()
Iterate the dict's values as a live view — not a copy. Insertion order preserved (3.7+).
Common call
for v in d.values():
Returns
a dict_values view — live, not a list
Replaces
the manual `[d[k] for k in d]` value-list
Watch out
not set-like (values may be unhashable); no direct membership hash speed-up
dict.values()
→ dict_values
Demo
Live evaluation
Try:
Inputs
dictdictkey: value pairs
Output
{'a': '1', 'b': '2', 'c': '3'}.values()
['1', '2', '3']
The demo materializes the view as a list so you can see the values at once. In real code you iterate directly: `for v in d.values():`. Since Python 3.7 the order matches insertion order of keys; earlier versions gave no guarantee. Duplicate values are preserved — values() is not a set.
Common patterns
Aggregate over values
sum, max, min, average — all one call away.
total = sum(d.values()) avg = sum(d.values()) / len(d)
Membership test on values
`x in d` tests KEYS; `x in d.values()` tests values.
if "admin" in d.values(): ...
Filter dict by value
Comprehension over items keeps the key/value link — do not iterate values() alone if you need the key too.
active = {k: v for k, v in d.items() if v}
Examples
1. Iterate values
list({"a": 1, "b": 2}.values())
Returns
[1, 2]2. Duplicates preserved
list({"a": 1, "b": 1}.values())
Returns
[1, 1] # unlike a set3. Empty dict
list({}.values())
Returns
[]4. Insertion order
list({"z": 1, "a": 2}.values())
Returns
[1, 2]5. Sum over values
sum({"a": 10, "b": 20}.values())
Returns
306. Membership test
"admin" in {"role": "admin"}.values()
Returns
TruePitfalls
1. It is a VIEW, not a list
values() returns a live view. Type checks that expect list, or indexing, both fail. Wrap in list() when you need a snapshot or index access.
Not indexable
d = {"a": 1, "b": 2} d.values()[0]
TypeError: 'dict_values' object is not subscriptable
Materialize
list(d.values())[0]
1
2. NOT set-like — unlike keys() and items()
Values may be unhashable and may repeat, so `d.values()` does not support union/intersection/difference. Only keys() and items() are set-like.
Type error
{"a": 1}.values() & {1, 2}
TypeError: unsupported operand type(s) for &: 'dict_values' and 'set'
Convert first
set({"a": 1}.values()) & {1, 2}
{1}
3. `x in d.values()` is O(n)
Value membership walks the values one by one — no hash speed-up. Big dicts with heavy value-membership testing want a separate reverse-index structure.
Slow on big dicts
if target in huge_dict.values(): ... # scans every value
O(n) per check
Reverse-index once
value_set = set(huge_dict.values()) if target in value_set: ...
O(1) per check after O(n) build
4. Modifying the dict during iteration raises
Changing the dict's size while iterating over its values view is a RuntimeError. Add/remove keys mid-loop — take a snapshot first.
Runtime error
for v in d.values(): if v is None: del d[find_key(d, v)]
RuntimeError: dictionary changed size during iteration
Snapshot first
for k in list(d): if d[k] is None: del d[k]
safe
When to use
Use it
- Aggregating values with sum / max / min / mean
- Membership testing (`x in d.values()`) on small dicts
- Feeding statistics / iteration pipelines with just the values
- "What values appear anywhere?" questions
Reach for something else
- Set-like operations → keys() or items() (values are not set-like)
- Heavy value membership on big dicts → build a set once
- Value → key lookup → build a reverse dict
- You also need the key → use d.items()
Notes
Complexity
O(1) to create the view; O(n) to iterate
Return
dict_values view — live, sized, iterable, NOT set-like
CPython impl
Objects/dictobject.c :: dictvalues_new — no data copied
Memory
O(1) — the view is a small wrapper over the dict
Thread-safe
Iteration is not safe under concurrent mutation of the source dict
FAQ
Keys in a dict are unique and hashable — that is what a set requires. Values may repeat and may be unhashable (lists, dicts, other unhashable types). So values cannot form a set without extra work.
History
2.2
values() introduced (originally as a list-building method).
3.0
values() became a view instead of a list; itervalues() was removed.
3.7
Insertion order preserved by dict — values() iterates in that order.