all Python 3Dictionaries (dict)function
dict.keys
iteration
Return a dynamic view of the dict's keys.
Syntax
d.keys()
The view is live, not a snapshot: add or drop a key after view = d.keys() and both the view and its len() follow the dict. So mutating the dict inside for k in d.keys() raises RuntimeError — iterate over a list(d.keys()) copy instead. Also, plain for k in d already walks the keys, so spelling out .keys() buys nothing.
Examples
d = {'a': 1, 'b': 2}
print(list(d.keys())) # → ['a', 'b']
view = d.keys()
d['c'] = 3
print(list(view)) # → ['a', 'b', 'c'] (view динамичен)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy