Spyros
Spyros

Reputation: 48606

Return a default value if a dictionary key is not available

I need a way to get a dictionary value if its key exists, or simply return None, if it does not.

However, Python raises a KeyError exception if you search for a key that does not exist. I know that I can check for the key, but I am looking for something more explicit. Is there a way to just return None if the key does not exist?


See also: Why dict.get(key) instead of dict[key]?

Upvotes: 789

Views: 721630

Answers (16)

Tim Pietzcker
Tim Pietzcker

Reputation: 336098

You can use dict.get()

value = d.get(key)

which will return None if key is not in d. You can also provide a different default value that will be returned instead of None (unless the dict actually contains None as the value for this key):

value = d.get(key, "empty")

Upvotes: 1326

jonrsharpe
jonrsharpe

Reputation: 121975

As mentioned elsewhere a possible issue with dct.get(key, default) is that, if key in dct but dct[key] is None, you get None not your default:

>>> dct = dict(foo=123, baz=None)
>>> dct.get("foo", 0)
123
>>> str(dct.get("bar", 0)
0
>>> dct.get("baz", 0)
>>> # where'd the value go?

Since Python 3.8, using the "walrus operator" to create an assignment expression, you can handle this like:

>>> value if (value := dct.get("baz")) is not None else 0
0

Upvotes: 5

Terminologist
Terminologist

Reputation: 983

If you have a more complex requirement that equates to a cache, this class might come in handy:

class Cache(dict):
    """ Provide a dictionary based cache

        Pass a function to the constructor that accepts a key and returns
        a value.  This function will be called exactly once for any key
        required of the cache.
    """

    def __init__(self, fn):
        super()
        self._fn = fn

    def __getitem__(self, key):
        try:
            return super().__getitem__(key)
        except KeyError:
            value = self[key] = self._fn(key)
            return value

The constructor takes a function that is called with the key and should return the value for the dictionary. This value is then stored and retrieved from the dictionary next time. Use it like this...

def get_from_database(name):
    # Do expensive thing to retrieve the value from somewhere
    return value

answer = Cache(get_from_database)
x = answer(42)   # Gets the value from the database
x = answer(42)   # Gets the value directly from the dictionary

Upvotes: 1

Aditya
Aditya

Reputation: 25

  1. d1={"One":1,"Two":2,"Three":3}

  2. d1.get("Four")

If you will run this code there will be no 'Keyerror' which means you can use 'dict.get()' to avoid error and execute your code

Upvotes: -1

Varmilo
Varmilo

Reputation: 25

You can use try-except block

try:
    value = dict['keyname']

except IndexError:
    value = None

Upvotes: -2

eaydin
eaydin

Reputation: 3252

For those using the dict.get technique for nested dictionaries, instead of explicitly checking for every level of the dictionary, or extending the dict class, you can set the default return value to an empty dictionary except for the out-most level. Here's an example:

my_dict = {'level_1': {
             'level_2': {
                  'level_3': 'more_data'
                  }
              }
           }
result = my_dict.get('level_1', {}).get('level_2', {}).get('level_3')
# result -> 'more_data'
none_result = my_dict.get('level_1', {}).get('what_level', {}).get('level_3')
# none_result -> None

WARNING: Please note that this technique only works if the expected key's value is a dictionary. If the key what_level did exist in the dictionary but its value was a string or integer etc., then it would've raised an AttributeError.

Upvotes: 4

Daenyth
Daenyth

Reputation: 37431

Use dict.get

Returns the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

Upvotes: 35

FastGTR
FastGTR

Reputation: 423

I was thrown aback by what was possible in python2 vs python3. I will answer it based on what I ended up doing for python3. My objective was simple: check if a json response in dictionary format gave an error or not. My dictionary is called "token" and my key that I am looking for is "error". I am looking for key "error" and if it was not there setting it to value of None, then checking is the value is None, if so proceed with my code. An else statement would handle if I do have the key "error".

if ((token.get('error', None)) is None):
    do something

Upvotes: 2

dusktreader
dusktreader

Reputation: 4045

You should use the get() method from the dict class

d = {}
r = d.get('missing_key', None)

This will result in r == None. If the key isn't found in the dictionary, the get function returns the second argument.

Upvotes: 35

imapotatoe123
imapotatoe123

Reputation: 696

A one line solution would be:

item['key'] if 'key' in item else None

This is useful when trying to add dictionary values to a new list and want to provide a default:

eg.

row = [item['key'] if 'key' in item else 'default_value']

Upvotes: 14

Evhz
Evhz

Reputation: 9238

If you can do it with False, then, there's also the hasattr built-in funtion:

e=dict()
hasattr(e, 'message'):
>>> False

Upvotes: -6

job
job

Reputation: 9253

I usually use a defaultdict for situations like this. You supply a factory method that takes no arguments and creates a value when it sees a new key. It's more useful when you want to return something like an empty list on new keys (see the examples).

from collections import defaultdict
d = defaultdict(lambda: None)
print d['new_key']  # prints 'None'

Upvotes: 27

martineau
martineau

Reputation: 123403

You could use a dict object's get() method, as others have already suggested. Alternatively, depending on exactly what you're doing, you might be able use a try/except suite like this:

try:
   <to do something with d[key]>
except KeyError:
   <deal with it not being there>

Which is considered to be a very "Pythonic" approach to handling the case.

Upvotes: 10

John La Rooy
John La Rooy

Reputation: 304137

Wonder no more. It's built into the language.

    >>> help(dict)

    Help on class dict in module builtins:

    class dict(object)
     |  dict() -> new empty dictionary
     |  dict(mapping) -> new dictionary initialized from a mapping object's
     |      (key, value) pairs
    ...
     |  
     |  get(...)
     |      D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
     |  
    ...

Upvotes: 73

Marek P
Marek P

Reputation: 477

As others have said above, you can use get().

But to check for a key, you can also do:

d = {}
if 'keyname' in d:

    # d['keyname'] exists
    pass

else:

    # d['keyname'] does not exist
    pass

Upvotes: 12

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76778

If you want a more transparent solution, you can subclass dict to get this behavior:

class NoneDict(dict):
    def __getitem__(self, key):
        return dict.get(self, key)

>>> foo = NoneDict([(1,"asdf"), (2,"qwerty")])
>>> foo[1]
'asdf'
>>> foo[2]
'qwerty'
>>> foo[3] is None
True

Upvotes: 26

Related Questions