Skip to content

Instantly share code, notes, and snippets.

@jsatt
Created April 20, 2021 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsatt/d844173e001be3eafbbcdad41f4617a7 to your computer and use it in GitHub Desktop.
Save jsatt/d844173e001be3eafbbcdad41f4617a7 to your computer and use it in GitHub Desktop.
Dot-notation access to deeply nested dict
def deep_get(path, data, default=None):
"""
Uses a provided dot-notation path to retrieve values from a deeply nested dict
"""
if isinstance(path, str):
path = path.split('.')
obj = data
for key in path:
obj = obj.get(key)
if obj is None:
return default
return obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment