get() looks up a key and returns its value. When the key is missing, it returns None, or the default you pass as the second argument, instead of raising KeyError like d[key] does.
It is a pure read: the dict is never changed, and the missing key is not added. Use it whenever a missing key is a normal situation, such as optional settings or counting things you have not seen yet.
dict.get() is an instance method of the built-in dict type: call it on a dictionary with dot notation, such as phone_book.get(...). No import is needed.
Syntax
Pass the key, and optionally a fallback value, as positional arguments.
python
value = d.get(key)
value = d.get(key, default)
Call
What it does
{"a": 1}.get("a")
Returns 1.
{"a": 1}.get("z")
Returns None; no error.
{"a": 1}.get("z", 0)
Returns 0; the dict is not changed.
Both parameters are positional only: d.get(key, 0) works, but d.get(key, default=0) raises TypeError.
Method signatures
python
dict.get(key, default=None, /)
# returns: d[key] if key is in d, otherwise default
Parameters
Parameter
Description
key
The key to look up. It must be hashable; an unhashable key such as a list raises TypeError, even with get().
default
Optional. The value returned when key is missing. Defaults to None. Pass it positionally.
Return Value
Return Value
Description
the stored value
When key exists, its value, exactly as d[key] would give it.
default
When key is missing: None, or the default you passed. The dict stays unchanged.
Python Examples
Example 1 — Look Up Phone Numbers Without KeyError
Compare get() with and without a default against square-bracket access on a small phone book.
555-0101
None
not listed
{'Asha': '555-0101', 'Ravi': '555-0199'}
KeyError for phone_book['Zara']
How It Works
Key present."Asha" exists, so get() returns her number, the same as phone_book["Asha"].
Key absent, no default."Zara" is not listed, so the call quietly returns None.
Custom fallback. The second argument replaces None with the friendlier text not listed.
Nothing inserted. Printing the dict shows only the two original entries: looking up Zara did not add her.
Brackets are strict.phone_book["Zara"] raises KeyError, which the except block turns into a message.
Example 2 — Count Words with get(word, 0) + 1
The classic counting idiom: read the current count with a default of 0, add one, and store it back.
python
text = "the cat sat on the mat the end"
counts = {}
for word in text.split():
counts[word] = counts.get(word, 0) + 1
print(counts)
top = max(counts, key=counts.get)
print(top, counts[top])
First sighting. The first time a word appears, counts.get(word, 0) returns 0, so the word is stored with a count of 1.
Repeat sighting. Later appearances read the stored count instead, so "the" climbs to 3.
Order of first appearance. Keys print in the order each word was first counted, because dicts keep insertion order.
get as a key function.max(counts, key=counts.get) compares keys by their counts and returns the most frequent word.
Example 3 — Tell a Stored None Apart from a Missing Key
A settings dict where timeout is deliberately None and proxy was never set.
python
config = {"timeout": None, "retries": 3}
print(config.get("timeout"), config.get("proxy"))
print("timeout" in config, "proxy" in config)
MISSING = object()
for key in ("timeout", "proxy"):
value = config.get(key, MISSING)
if value is MISSING:
print(key, "-> not set")
else:
print(key, "->", value)
Output
None None
True False
timeout -> None
proxy -> not set
How It Works
Same answer, different reasons. Both lookups print None: one key holds None, the other does not exist.
Membership is exact.in checks keys only, so it reports that timeout exists and proxy does not.
Sentinel default.object() creates a unique value that can never be stored by accident, so value is MISSING means the key was absent.
Honest report. The loop prints the real stored None for timeout and "not set" for proxy.
Frequently Asked Questions
It returns None, or the default you pass as the second argument, such as d.get("age", 0). It never raises KeyError.
d[key] raises KeyError when the key is missing. d.get(key) returns None or your default instead. Both return the same value when the key exists.
No. get() only reads. If you want the default stored in the dict as well, use setdefault(key, default).
The parameters of get() are positional only, so it accepts no keyword arguments. Write d.get(key, 0) instead.
Check key in d, or pass a unique sentinel such as MISSING = object() as the default and test the result with is.