-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry.py
More file actions
30 lines (28 loc) · 795 Bytes
/
Copy pathtry.py
File metadata and controls
30 lines (28 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def recursive_search(data, target):
for key, value in data.items():
if key == target:
return value
elif isinstance(value, dict):
return recursive_search(value, target)
elif isinstance(value, list):
for item in value:
if isinstance(item, dict):
result = recursive_search(item, target)
if result:
return result
return None
nested_dict = {
"level1": {
"level2": {
"level3a": 7,
"level3b": ["red", {"target_key": "hello, world"}]
},
"another_level2": {
"level3": 8
}
},
"another_level1": {
"level2": 9
}
}
print(recursive_search(nested_dict, "target_key"))