Created
February 27, 2018 06:48
-
-
Save rivmar/8169b2353dfcaac85a0e14c6f3e53a99 to your computer and use it in GitHub Desktop.
[Python] Recursive search in nested dict by key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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