-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
21 lines (19 loc) · 778 Bytes
/
utils.py
File metadata and controls
21 lines (19 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def find(key, dictionary):
"""
Generator to extract items from complex nested data structures
source: https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-python-dictionaries-and-lists
:param key: Key to look for
:param dictionary: Object with nested dicts and lists
:return: generator with all values matching the desired key
"""
for k, v in dictionary.items():
if k == key:
yield v
elif isinstance(v, dict):
for result in find(key, v):
yield result
elif isinstance(v, list):
for d in v:
if isinstance(d, dict) or isinstance(d, list):
for result in find(key, d):
yield result