def flt(x):
if x is None:
return False
try:
return x > 1
except TypeError:
return len(x)
def recursive_test(leaf, fn):
if isinstance(leaf, list):
if fn(leaf[0]):
return leaf
else:
return None
else:
_leaf = {}
try:
for key, val in leaf.items():
cur_leaf = recursive_test(val, fn)
if cur_leaf:
_leaf[key] = cur_leaf
return _leaf or None
except AttributeError:
if fn(leaf):
return leaf
a = {
'a': None,
'b': 4,
'c': {
'd': None,
'e': "test",
'f': {
'g': 1, # will get filtered by flt
'h': None,
'i': '' # will get filtered by flt
}
}
}
print(recursive_test(a, flt))