Skip to content
Advertisement

Python: Safe dictionary access with lists?

Is there an exception free way to access values from a dictionary containing lists. For example, if I have:

data = {
    "object_1": {
        "object_2": {
            "list": [
                {
                    "property": "hello"
                }
            ]
        }
    }
}

How do I access the path data['object_1']['object_2']['list'][0]['property'] safely(i.e. return some default value if not possible to access without throwing error)? I am trying to avoid wrapping these in try-except‘s. I have seen the reduce based approach but it doesn’t take into account having lists inside the dictionary.

In JS, I can write something like:

data.object_1?.object_2?.list[0]?.property ?? 'nothing_found'

Is there something similar in Python?

Advertisement

Answer

For dict you can use the get method. For lists you can just be careful with the index:

data.get('object_1', {}).get('object_2', {}).get('list', [{}])[0].get('property', default)

This is a bit awkward because it makes a new temporary dict or lost for each call get. It’s also not super safe for lists, which don’t have an equivalent method.

You can wrap the getter in a small routine to support lists too, but it’s not really worth it. You’re better off writing a one-off utility function that uses either exception handling or preliminary checking to handle the cases you want to react to:

def get(obj, *keys, default=None):
    for key in keys:
        try:
            obj = obj[key]
        except KeyError, IndexError:
            return default
    return obj

Exception handing has a couple of huge advantages over doing it the other way. For one thing, you don’t have to do separate checks on the key depending on whether the object is a dict or list. For another, you can support almost any other reasonable type that supports __getitem__ indexing. To show what I mean, here is the asking for permission rather than forgiveness approach:

from collections.abc import Mapping, Sequence
from operator import index

def get(obj, *keys, default=None):
    for key in keys:
        if isinstance(obj, Mapping):
            if key not in obj:
                return default
        elif isinstance(obj, Sequence):
            try:
                idx = index(key)
            except TypeError:
                return default
            if len(obj) <= idx or len(obj) < -idx:
                 return default
        obj = obj[key]
    return obj

Observe how awkward and error-prone the checking is. Try passing in a custom object instead of a list, or a key that’s not an integer. In Python, carefully used exceptions are your friend, and there’s a reason it’s pythonic to ask for forgiveness rather than for permission.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement