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:

JavaScript

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:

JavaScript

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:

JavaScript

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:

JavaScript

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:

JavaScript

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