I have an arbitrarily deep set of nested dictionary:
x = {\'a\': 1, \'b\': {\'c\': 6, \'d\': 7, \'g\': {\'h\': 3, \'i\': 9}}, \'e\': {\'f\': 3}}
<
If you need it to work for both lists and dicts in arbitrary nesting:
def apply_recursive(func, obj):
if isinstance(obj, dict): # if dict, apply to each key
return {k: apply_recursive(func, v) for k, v in obj.items()}
elif isinstance(obj, list): # if list, apply to each element
return [apply_recursive(func, elem) for elem in obj]
else:
return func(obj)