Skip to content

Instantly share code, notes, and snippets.

@gigkokman
Last active November 9, 2020 14:14
Show Gist options
  • Save gigkokman/a85c808b98b94e98830e4b931bb1e225 to your computer and use it in GitHub Desktop.
Save gigkokman/a85c808b98b94e98830e4b931bb1e225 to your computer and use it in GitHub Desktop.
Python function get dictionary's value from nested with dot annotation key
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