Skip to content

Instantly share code, notes, and snippets.

@rubdottocom
Created February 22, 2019 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubdottocom/eb5b284909087222de5e77abce094cbb to your computer and use it in GitHub Desktop.
Save rubdottocom/eb5b284909087222de5e77abce094cbb to your computer and use it in GitHub Desktop.
Find all occurrences of a key in nested python dictionaries
def gen_dict_extract(node, kv):
if isinstance(node, list):
for i in node:
for x in gen_dict_extract(i, kv):
yield x
elif isinstance(node, dict):
if kv in node:
yield node[kv]
for j in node.values():
for x in gen_dict_extract(j, kv):
yield x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment