Last active
September 27, 2024 16:12
-
-
Save rnetonet/e2ef3e3e02eb70ff9f5b8e9e44d8331d to your computer and use it in GitHub Desktop.
Function to traverse a complex object looking for the first occurence of a determined key (search) in a dict
This file contains hidden or 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 t(complext_object, search): | |
| """ | |
| Traverse dictionary to find a key and return its first occurrence value. | |
| """ | |
| if isinstance(complext_object, list): | |
| for item in complext_object: | |
| ret = t(item, search) | |
| if ret: | |
| return ret | |
| elif isinstance(complext_object, dict): | |
| for k, v in complext_object.items(): | |
| if k == search: | |
| return v | |
| else: | |
| ret = t(v, search) | |
| if ret: | |
| return ret | |
| return dict() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment