Skip to content

Instantly share code, notes, and snippets.

@antimirov
Last active July 30, 2019 13:13
Show Gist options
  • Select an option

  • Save antimirov/b1ba9ae6d64aef7bcddff12423af41c5 to your computer and use it in GitHub Desktop.

Select an option

Save antimirov/b1ba9ae6d64aef7bcddff12423af41c5 to your computer and use it in GitHub Desktop.
def gen_dict_extract(key, var, path=None):
if path is None:
path = []
if hasattr(var, "items"):
for k, v in var.items():
if k == key:
yield v, path + [k]
if isinstance(v, dict):
for result, _path in gen_dict_extract(key, v, path + [k]):
yield result, _path
elif isinstance(v, list):
for idx, d in enumerate(v):
for result, _path in gen_dict_extract(key, d, path + [f"{k}[{idx}]"]):
yield result, _path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment