Skip to content

Instantly share code, notes, and snippets.

@craigderington
Created April 27, 2022 02:32
Show Gist options
  • Save craigderington/733bf74874b252322a5a4fca02d21dd3 to your computer and use it in GitHub Desktop.
Save craigderington/733bf74874b252322a5a4fca02d21dd3 to your computer and use it in GitHub Desktop.
Recursive Dictionary Get
def recursive_dict_get(d, *keys, default_none=False):
"""Recursive dict get. Can take an arbitrary number of keys and returns an empty
dict if any key does not exist. https://stackoverflow.com/a/28225747"""
ret = reduce(lambda c, k: c.get(k, {}), keys, d)
if default_none and ret == {}:
return None
else:
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment