iteration
dict.values
Returns a dynamic view object of the dictionary's values. Unlike keys(), the values view is not set-like because values may be non-hashable or duplicated.
d.values() Parameters
| Parameter | Purpose |
|---|---|
| (no parameters) | values() takes no arguments |
| returns | dict_values view — iterable, len-able, not set-like |
Examples
>>> d = {'a': 1, 'b': 2, 'c': 2}
>>> list(d.values())
[1, 2, 2] Duplicates are preserved
>>> sum({'a': 10, 'b': 20, 'c': 30}.values())
60 Directly feed views into sum/min/max
>>> d = {'x': 1}
>>> vs = d.values()
>>> d['y'] = 2
>>> list(vs)
[1, 2] View updates live as the dict changes
>>> 2 in {'a': 1, 'b': 2}.values()
True Membership test — O(n) because values aren't hashed for lookup
Gotcha
values() has no set operations (& | - ^) since values may be unhashable. Membership checks with 'in' are O(n), unlike keys which are O(1).
Related methods
dict.keys
Returns a dynamic view object of the dictionary's keys. The view reflects any subsequent changes made to the dictionary and supports set-like operations.
dict.items
Returns a dynamic view of (key, value) tuple pairs. The view is set-like when all values are hashable and reflects subsequent changes to the dictionary.
key in dict
Membership test invoking __contains__ — returns True if key exists in the dictionary. The check is against keys only (never values) and runs in O(1) average time.
← All Python dict methods · String methods · Built-in functions