Python Glossary Русский

all Python 3Built-in functionsterm

dict

types

A mutable mapping: stores key–value pairs. Keys are unique and hashable. Since Python 3.7 it preserves insertion order.

Syntax

dict(**kwargs)
dict(mapping)
dict(iterable)

Indexing a missing key with d[k] raises KeyError; when the key may be absent, use d.get(k) or d.get(k, default) instead. Keys must be hashable, so a tuple can be a key but a list cannot. Lookup and insertion are O(1) on average, and equality ignores insertion order — {'a':1,'b':2} == {'b':2,'a':1} is True even though iteration order differs.

Examples

print(dict(a=1,b=2))  # → {'a':1,'b':2}
print(dict([('x',1),('y',2)]))  # → {'x':1,'y':2}
print(dict.fromkeys(['a','b'],0))  # → {'a':0,'b':0}
d=dict(zip('abc',[1,2,3]))
print(d)  # → {'a':1,'b':2,'c':3}
print(dict({'a':1},b=2))  # → {'a':1,'b':2}

See also

Open in the full glossarydocs.python.orgReport an inaccuracy