Python Glossary Русский

all Python 3Dictionaries (dict)function

dict.items

iteration

Return a dynamic view of (key, value) pairs; reflects later changes to the dict.

Syntax

d.items()

It is a live window into the dict, not a list: adding or removing keys while looping over it raises RuntimeError, so snapshot the pairs into a list first if you intend to modify. The view is also set-like — intersecting or subtracting two item views gives the shared and differing pairs, as long as the values are hashable.

Examples

d = {'a': 1, 'b': 2}
print(list(d.items()))   # → [('a', 1), ('b', 2)]
print(dict(d.items()))   # → {'a': 1, 'b': 2}

See also

Open in the full glossarydocs.python.orgReport an inaccuracy