Created
April 20, 2021 20:27
-
-
Save jsatt/d844173e001be3eafbbcdad41f4617a7 to your computer and use it in GitHub Desktop.
Dot-notation access to deeply nested dict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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