Python Function Reference

dict.get

Get the value for a key without an error for a missing key.

Python 3.13
dict.get(key[, default])
  • [ ]optional part
key
The key whose value to get.
Hashable
default
What to return if the key is missing, None by default.
AnyOptional

Return value

The value for the key, or default if the key is missing; None without default.

Examples

Default value for a missing key

Python 3.13
a = {'x': 1, 'y': 2}
print(a.get('z', 0))
0

Lookup without a default

Python 3.13
a = {'x': 1}
print(a.get('x'))
print(a.get('z'))
1
None

Details

Unlike d[key], it does not raise KeyError and does not add the key to the dictionary.

See also

Where to learn