dict · Python

get

Returns the value, or a default when the key is missing, without raising.

dict.get(key, default=None, /)

Complexity

AverageO(1)
Worst caseO(n)
Extra spaceO(1)

Same lookup as d[key].

Tested example

ages = {"ada": 36}
assert ages.get("ada") == 36
assert ages.get("nobody") is None  # no exception
assert ages.get("nobody", 0) == 0  # explicit default
Runs in the test suite on CPython 3.14.0 · examples/python/tests/test_collections_dict.py

Parameters

key
The key.
default
Returned when missing (None if omitted).

Behaviour

Returns
The value or the default.
Errors and preconditions
TypeError for an unhashable key.
Changes
Nothing (it does not insert the default; setdefault does).
Memory and invalidation
No allocation.