Skip to content Skip to sidebar Skip to footer

Efficient Way To Check If Dictionary Key Exists And Has Value

Let's say there's a dictionary that looks like this: d = {'key1': 'value1', 'key2': {'key3': 'value3'}} The dictionary may or may not contain key2, also key2 might be empty. So in

Solution 1:

You can use .get() with default value:

val = d.get("key2", {}).get("key3", None)  # <-- you can put something else instead of `None`, this value will return if key2 or key3 doesn't exist

For example:

d = {"key1": "value1", "key2": {"key3": "value3"}}

val = d.get("key2", {}).get("key3", None)

if not valis None:
    print(val)
else:
    print("Not found.")

Solution 2:

One approach would be to expect the failure and catch it:

try:
    value = d['key2']['key3']
except (KeyError, TypeError):
    pass

(don't call your variable the name of the type, it's a bad practice, I've renamed it d)

The KeyError catches a missing key, the TypeError catches trying to index something that's not a dict.

If you expect this failure to be very common, this may not be ideal, as there's a bit of overhead for a try .. except block.

In that case you're stuck with what you have, although I'd write it as:

if'key2'in d and d['key2'] and 'key3'in d['key2']:
    value = d['key2']['key3']

Or perhaps a bit more clearly:

if'key2'in d andisinstance(d['key2'], dict) and'key3'in d['key2']:
    value = d['key2']['key3']

If you're about to assign something else to value in the else part (like None), you could also consider:

value = d['key2']['key3'] if 'key2' in d and d['key2'] and 'key3' in d['key2'] else None
    

Post a Comment for "Efficient Way To Check If Dictionary Key Exists And Has Value"