dict.get()

Safe key lookup: the value if the key exists, a default if it does not — never a KeyError.

Dict methodPython 2.0+Live demo
Common call
config.get("debug", False)
Returns
value or default (None if omitted)
Replaces
d[key] raises on a missing key; get never does
Watch out
a key stored with value None is indistinguishable from missing
dict.get(keykeyThe key to look up.type: hashable · required, defaultdefaultReturned when the key is absent. Not stored in the dict.type: Any · default: None=None)
Any

Demo

Live evaluation
Try:
Inputs
dictdictkey: value pairs
keystrkey to look up
defaultAnyempty = None
Output
{'a': '1', 'b': '2'}.get('a')
'1'

If the key exists you get its value; if not, you get the default — None when no default is given. The dict itself is never modified. (Demo values are strings, from the text inputs.)

Parameters

NameTypeRequiredDescription
keyhashableyesThe key to look up.
defaultAnyno (None)Returned when the key is absent. Not stored in the dict.

Return value

AnyThe value for key if present, otherwise default. Never raises KeyError.

Common patterns

Optional config with fallback
The canonical use — absent settings fall back to a sane default.
timeout = config.get("timeout", 30)
Counting occurrences
get with a 0 default replaces the missing-key dance.
counts[word] = counts.get(word, 0) + 1
Chained optional lookups
Defaulting to an empty dict keeps the chain alive.
city = user.get("address", {}).get("city")

Examples

1. Key present
{"a": 1}.get("a")
Returns
1
2. Key missing — default None
{"a": 1}.get("z")
Returns
None
3. Key missing — explicit
{"a": 1}.get("z", 0)
Returns
0

Pitfalls

1. None value vs missing key
get cannot distinguish a stored None from an absent key.
Ambiguous
{"a": None}.get("a") is {"x": 1}.get("a")
True — both None
Check membership
if "a" in d:
    value = d["a"]
presence and value handled separately
2. The default is not stored
get reads; it never writes. Use setdefault to store the fallback.
Not saved
d.get("k", [])
print(d)
{} — still empty
Fix
d.setdefault("k", []).append(x)
{'k': [x]}
3. Eager default evaluation
The default expression runs even when the key exists.
Wasted work
d.get(k, expensive())
expensive() always called
Fix
d[k] if k in d else expensive()
expensive() only on miss

When to use

Use it
  • Lookups where missing keys are normal, not errors
  • Config and options dictionaries
  • Counting / accumulating patterns
Reach for something else
  • A missing key is a bug → d[key] so it raises loudly
  • Store the default on miss → dict.setdefault
  • Same default for every key → collections.defaultdict

Notes

Complexity
O(1) average — hash lookup
Return
value or default; dict unchanged
CPython impl
Objects/dictobject.c :: dict_get_impl
Memory
No allocation
Thread-safe
Single lookup is atomic in CPython; read-modify-write is not

FAQ

When a missing key means something went wrong. Square brackets raise KeyError immediately — a silent None can travel far before it explodes.

History

2.0
Core dict method, unchanged semantics since.