Python Dictionary get() Method

The dictionary get() method lets you safely read a value from a dictionary.

It is useful when a key might be missing. Instead of crashing with a KeyError, get() returns None by default, or a custom fallback value if you provide one.

person = {"name": "Ana", "age": 25}

print(person.get("name"))           # Ana
print(person.get("city"))           # None
print(person.get("city", "N/A"))    # N/A

Use get() when a dictionary key may be missing and you want a safe result instead of a KeyError.

What get() does #

The get() method:

  • Returns the value for a given key
  • Returns None if the key does not exist
  • Can return a custom default value instead
  • Works on dictionary objects only

Example:

student = {"name": "Lina", "grade": 90}

print(student.get("name"))     # Lina
print(student.get("email"))    # None

In this example:

  • "name" exists, so its value is returned
  • "email" does not exist, so get() returns None

If you are still learning how dictionaries work, see Python dictionaries explained.

Basic syntax #

There are two common forms of get():

dictionary.get(key)
dictionary.get(key, default_value)

What each part means:

  • key is the dictionary key you want to look up
  • default_value is optional
  • If you do not provide a default, Python uses None

Example:

settings = {"theme": "dark"}

print(settings.get("theme"))            # dark
print(settings.get("language"))         # None
print(settings.get("language", "en"))   # en

When to use get() #

Use get() when:

  • A key might not exist
  • You want to avoid KeyError
  • You are reading optional data
  • You are working with JSON, user input, or config data

Example with optional user data:

user = {"username": "sam", "email": "sam@example.com"}

print(user.get("username"))              # sam
print(user.get("phone"))                 # None
print(user.get("phone", "Not provided")) # Not provided

This is common when some fields are optional.

If you want more help with reading dictionary values, see how to access values in a dictionary in Python.

get() vs square brackets #

You can access dictionary values in two common ways:

data["key"]
data.get("key")

The difference is important.

Square brackets #

Square brackets are strict. If the key is missing, Python raises a KeyError.

data = {"name": "Ana"}

print(data["name"])   # Ana
print(data["city"])   # KeyError

get() #

get() is safe for missing keys.

data = {"name": "Ana"}

print(data.get("name"))   # Ana
print(data.get("city"))   # None

Use:

  • Square brackets when the key must exist
  • get() when the key is optional

If you are seeing crashes from missing keys, read KeyError in Python: causes and fixes.

Using a default value #

You can pass a second argument to get() to choose what should be returned if the key is missing.

profile = {"name": "Mia"}

print(profile.get("country", "Unknown"))  # Unknown

This is useful for display text:

  • "Unknown"
  • "Not set"
  • "N/A"

It is also useful in calculations:

scores = {"math": 10, "science": 8}

total_history = scores.get("history", 0)
print(total_history)   # 0

Here, 0 is a good default because the code expects a number.

Try to choose a default value that matches the type you expect:

  • Use a string for text
  • Use 0 for numbers
  • Use [] for lists if needed
  • Use {} for dictionaries if needed

Important beginner notes #

There are a few things beginners often miss about get().

get() does not add a missing key #

This code does not create a new key:

person = {"name": "Ana"}

print(person.get("city"))  # None
print(person)              # {'name': 'Ana'}

The dictionary stays the same.

get() does not change the dictionary #

It only reads a value safely.

settings = {"mode": "light"}

settings.get("theme", "dark")
print(settings)   # {'mode': 'light'}

Even though "dark" was returned as a default, it was not saved into the dictionary.

A stored value can also be None #

Sometimes get() returns None because:

  • The key is missing, or
  • The key exists and its value is actually None

Example:

data = {"name": "Ana", "nickname": None}

print(data.get("nickname"))  # None
print(data.get("city"))      # None

Both lines print None, but they mean different things.

If you need to know whether the key exists, check with in:

data = {"name": "Ana", "nickname": None}

print("nickname" in data)  # True
print("city" in data)      # False

For a full beginner guide, see how to check if a key exists in a dictionary in Python.

Common mistakes #

Here are some common problems when using get():

  • Using square brackets for optional keys and getting a KeyError
  • Forgetting that get() returns None if no default is provided
  • Assuming get() creates a key automatically
  • Using a default value of the wrong type

Debugging steps that help:

print(my_dict)
print(my_dict.keys())
print('name' in my_dict)
print(my_dict.get('name'))
print(type(my_dict.get('name')))

What these checks tell you:

  • print(my_dict) shows the full dictionary
  • print(my_dict.keys()) shows which keys exist
  • print('name' in my_dict) checks if a specific key exists
  • print(my_dict.get('name')) shows the returned value
  • print(type(my_dict.get('name'))) shows the value type

You may also want to learn about the dictionary keys() method and the dictionary items() method when inspecting dictionary contents.

FAQ #

Does get() raise KeyError? #

No. If the key is missing, it returns None or the default value you provide.

Does get() add a key to the dictionary? #

No. It only reads a value safely.

What is the default return value of get()? #

None, unless you pass a second argument.

Should I use get() or square brackets? #

Use get() for optional keys. Use square brackets when the key must exist.

See also #

Press Esc to close