Python dict.get() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
dict method

Definition

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)
CallWhat 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

ParameterDescription
keyThe key to look up. It must be hashable; an unhashable key such as a list raises TypeError, even with get().
defaultOptional. The value returned when key is missing. Defaults to None. Pass it positionally.

Return Value

Return ValueDescription
the stored valueWhen key exists, its value, exactly as d[key] would give it.
defaultWhen 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.

python
phone_book = {"Asha": "555-0101", "Ravi": "555-0199"}

print(phone_book.get("Asha"))
print(phone_book.get("Zara"))
print(phone_book.get("Zara", "not listed"))
print(phone_book)
try:
    print(phone_book["Zara"])
except KeyError:
    print("KeyError for phone_book['Zara']")

How It Works

  1. Key present. "Asha" exists, so get() returns her number, the same as phone_book["Asha"].
  2. Key absent, no default. "Zara" is not listed, so the call quietly returns None.
  3. Custom fallback. The second argument replaces None with the friendlier text not listed.
  4. Nothing inserted. Printing the dict shows only the two original entries: looking up Zara did not add her.
  5. 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])

How It Works

  1. First sighting. The first time a word appears, counts.get(word, 0) returns 0, so the word is stored with a count of 1.
  2. Repeat sighting. Later appearances read the stored count instead, so "the" climbs to 3.
  3. Order of first appearance. Keys print in the order each word was first counted, because dicts keep insertion order.
  4. 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)

How It Works

  1. Same answer, different reasons. Both lookups print None: one key holds None, the other does not exist.
  2. Membership is exact. in checks keys only, so it reports that timeout exists and proxy does not.
  3. Sentinel default. object() creates a unique value that can never be stored by accident, so value is MISSING means the key was absent.
  4. 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.

Next: dict.items()

Return a live view of (key, value) pairs for loops, sorting, and comparisons.

items() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful