Overview TL; DR
I wrote a navigateDict function that makes dict safe navigation, similar to dict.get() but nested. It replaces the code, for example
if 1 in data and 'i' in data[1] and 'a' in data[1]['i']: print data[1]['i']['a'] else: print "Not found"
with roughly equivalent
found = navigateDict(data, 1, 'i', 'a') if found is not None: print found else: print "Not found"
- Anything like this part of the standard library already?
- Is there a more idiomatic way to do the same?
- Any answer that requires entering any key of the path component more than once probably does not respond.
Additional Information
The implementation is as follows:
# Allow fallback value other than None def navigateDictEx(d, keys, fallback=None): for key in keys: if key in d: d = d[key] else: return fallback return d def navigateDict(d, *keys): return navigateDictEx(d, keys)
See summary, for example, use.
Pythonic or not, this feature reduces repetition in a place where redundancy is a bad idea. For example, when changing one component of a path in an example, up to three different values ββare required, which must be changed as one in the original example, but only one in the modified example. Given my usual tendency to make mistakes, this is a big win.
Ultimately, I ask: is there something in the standard library that does this, or do I need to find a place for it in my project library?
If hits are expected to dominate misses
brionius correctly indicates that the KeyError trap will work:
try: print data[1]['i']['a'] except KeyError: print "Not found"
It may be the way I go; it is quite short and cuts the repetition. However, it reflects the assumption that there will be more hits than misses. If there is a better way to accept the opposite, I would also like to know.