update
This commit is contained in:
26
src/utils.py
Normal file
26
src/utils.py
Normal file
@@ -0,0 +1,26 @@
|
||||
def find_key(obj: dict | list[dict], key: str) -> list:
|
||||
"""
|
||||
Find all values of a given key within a nested dict or list of dicts
|
||||
|
||||
@param obj: dictionary or list of dictionaries
|
||||
@param key: key to search for
|
||||
@return: list of values
|
||||
"""
|
||||
def helper(obj: any, key: str, L: list) -> list:
|
||||
if not obj:
|
||||
return L
|
||||
|
||||
if isinstance(obj, list):
|
||||
for e in obj:
|
||||
L.extend(helper(e, key, []))
|
||||
return L
|
||||
|
||||
if isinstance(obj, dict) and obj.get(key):
|
||||
L.append(obj[key])
|
||||
|
||||
if isinstance(obj, dict) and obj:
|
||||
for k in obj:
|
||||
L.extend(helper(obj[k], key, []))
|
||||
return L
|
||||
|
||||
return helper(obj, key, [])
|
||||
Reference in New Issue
Block a user