Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get Nested Values from dict
def get_nested_values_from_dict(data, attrs, index=0):
"""
get values from nested dictionary
Example:
>>> data = {"a": {"b": {"c": 3}}
>>> get_nested_values_from_dict(data, ["a", "b", "c"])
>>> 3
:param data: dict
:param attrs: list
:param index: int
:return:
"""
if index >= len(attrs):
return data
try:
return get_nested_values_from_dict(
data.get(attrs[index]), attrs, index + 1
)
except AttributeError:
raise ValueError("Invalid parameters passed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.