Last active
November 9, 2020 14:14
-
-
Save gigkokman/a85c808b98b94e98830e4b931bb1e225 to your computer and use it in GitHub Desktop.
Python function get dictionary's value from nested with dot annotation key
This file contains hidden or 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
from functools import reduce | |
def get_nested_value(nested_dict, str_key): | |
""" | |
Get value from nested-dict via dot-annotation key | |
Parameters: | |
nested_dict: dict | |
str_key: str | |
Example: | |
nested = {"a": {"aa": "aaa"}} | |
key = "a.aa" | |
aa = get_nested_value(nested, key) | |
""" | |
return reduce(lambda data, key: data.get(key), str_key.split('.'), nested_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment