from collections import OrderedDict
from pprint import pprint
def recursive_test(leaf, fn, depth=0):
if isinstance(leaf, list):
_tree = []
for i,j in enumerate(leaf):
if recursive_test(j, fn, depth=depth+1):
_tree.append(j)
return _tree
if isinstance(leaf, tuple):
return fn(leaf[1])
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', []),
('b', [OrderedDict(), OrderedDict(), OrderedDict(), OrderedDict(), OrderedDict()]),
('c', None), ('d', None), ('e', None), ('f', None), ('g', None),
('h', 'none'), ('i', 'lorem ipsum'), ('j', None),
('k', 'none'), ('l', OrderedDict()), ('m', None), ('n', '1234 test 2'),
('o', 1234), ('p', 'asdfds'), ('q', 'fewrwer'), ('r', 'asdfewfwef'),
('s', 'ahfgdfg'), ('t', 'werewr'), ('u', 'werwefwef'), ('v', '23fdwokf')]
pprint(recursive_test(a, lambda x:x is not None))