Skip to content

Instantly share code, notes, and snippets.

@rivmar
Created February 27, 2018 06:48
Show Gist options
  • Save rivmar/8169b2353dfcaac85a0e14c6f3e53a99 to your computer and use it in GitHub Desktop.
Save rivmar/8169b2353dfcaac85a0e14c6f3e53a99 to your computer and use it in GitHub Desktop.
[Python] Recursive search in nested dict by key
def get_all(data, key):
if type(data) is dict:
for jsonkey in data.keys():
if jsonkey == key:
return data[jsonkey]
#Return the first pair only!
break
elif type(data[jsonkey]) in (list, dict):
if get_all(data[jsonkey], key):
return get_all(data[jsonkey], key)
elif type(data) is list:
for item in data:
if type(item) in (list, dict):
if get_all(item, key):
return get_all(item, key)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment