Python Glossary Русский

all Python 3Dictionaries (dict)function

dict.get

access

Return the value for a key, or default (None by default), without raising KeyError.

Syntax

d.get(key, default=None)

get cannot tell "key missing" from "key present with value None" — both give None, so use key in d when the difference matters. The default argument is evaluated eagerly, before the call happens: d.get(k, expensive()) runs expensive() even when the key is there. Unlike setdefault, get never modifies the dictionary.

Examples

d = {'a': 1}
print(d.get('a'))      # → 1
print(d.get('z'))      # → None
print(d.get('z', 0))   # → 0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy