Return a default value if Key is not in Dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Jun 15, 2022

banner

Photo from Unsplash

Return a default value if Key is not in Dictionary in Python #

Use the dict.get() method to return a default value if a key is not in the dictionary, e.g. my_dict.get('key_name', 'default'). The dict.get() method returns the value for the given key if the key is in the dict, otherwise the default value is returned.

main.py
my_dict = {'name': 'Alice', 'age': None} # 👇️ without providing default value print(my_dict.get('country')) # 👉️ None # 👇️ with a default value print(my_dict.get('country', 'Austria')) # 👉️ 'Austria'

The dict.get method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.

The method takes the following 2 arguments:

NameDescription
keyThe key for which to return the value
defaultThe default value to be returned if the provided key is not present in the dictionary (optional)

If a value for the default parameter is not provided, it defaults to None, so the get() method never raises a KeyError.

Note that if the key exists in the dictionary and has a value of None, the dict.get() method will return None even if you provide a default value.
main.py
my_dict = {'name': 'Alice', 'age': None} print(my_dict.get('age', 30)) # 👉️ None print(my_dict.get('age', 100)) # 👉️ None

We provided a default value to the dict.get() method but the age key is present in the dictionary, so its value is returned.

One way to deal with this is to use a conditional expression to reassign the variable if it stores None.

main.py
my_dict = {'name': 'Alice', 'age': None} result = my_dict.get('age', 30) print(result) # 👉️ None result = 30 if result is None else result print(result) # 👉️ 30

If the result variable stores a None value, we set it to 30, otherwise we set it to its current value.

An alternative approach is to use the boolean or operator.

main.py
my_dict = {'name': 'Alice', 'age': None} result = my_dict.get('age', 30) or 30 print(result) # 👉️ 30

However, this approach does not explicitly check for None.

The boolean or operator will return the value to the right if the value to the left is falsy.

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 0 (zero) of any numeric type
  • empty sequences and collections: "" (empty string), () (empty tuple), [] (empty list), {} (empty dictionary), set() (empty set), range(0) (empty range).

So if the value to the left is any of the aforementioned falsy values, the value to the right is returned.

main.py
print(0 or 'default value') # 👉️ default value print("" or 'default value') # 👉️ default value

This might or might not suit your use case.

If you only want to check for None, use the conditional expression from the previous code snippet.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.