Python Glossary Русский

all Python 3Dictionaries (dict)construct

d[key] = value

access

Assigning d[key] = value adds a new key or replaces an existing value. For several pairs at once, use dict.update().

Syntax

d[key] = value

The key must be hashable: strings, numbers and tuples of immutable items work, while a list or a set raises TypeError: unhashable type. Since Python 3.7 insertion order is guaranteed, and overwriting an existing key keeps its original position rather than moving it to the end — to push a key to the back you have to delete it and insert it again.

Examples

d = {'a': 1}
d['b'] = 2
print(d)   # → {'a': 1, 'b': 2}
d['a'] = 99
print(d)   # → {'a': 99, 'b': 2}

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy