Python Glossary Русский

Python 3.3+collectionsterm

collections.abc.ValuesView

collections.abc — protocols

The type of a dict values view (d.values()).

Syntax

isinstance(obj, collections.abc.ValuesView)

It is a live view, not a copy: change the dict and the view changes with it, and adding or removing keys while iterating over it raises RuntimeError. Unlike keys and items views, ValuesView is not set-like (no &, |, -), because values may repeat and may be unhashable. There is no indexing or slicing either — wrap it in list() if you need random access.

Examples

import collections.abc as abc
print(isinstance({}.values(), abc.ValuesView))   # → True
d = {'a': 1, 'b': 2}
print(list(d.values()))   # → [1, 2]
print(len(d.values()), sum(d.values()))   # → 2 3
print(isinstance(d.keys(), abc.ValuesView), isinstance(d.keys(), abc.KeysView))   # → False True
v = d.values()
d['c'] = 10
print(list(v))   # → [1, 2, 10]
print(d.values()[0])   # → TypeError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy