Question: 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}}

```

and I'd like to basically apply a function to all the integers in the dictionaries, so like `map`, I guess, but for nested dictionaries.

So: `map_nested_dicts(x, lambda v: v + 7)` would be the sort of goal.

I'm stuck as to the best way to perhaps store the layers of keys to then put the modified value back into its correct position.

What would the best way/approach to do this be?

Answer: 
Just to expand on vaultah's answer, if one of your elements can be a list, and you'd like to handle those too:

```
import collections

def map_nested_dicts_modify(ob, func):
for k, v in ob.iteritems():
    if isinstance(v, collections.Mapping):
        map_nested_dicts_modify(v, func)
    elif isinstance(v, list):
        ob[k] = map(func, v)
    else:
        ob[k] = func(v)

```
If you want to avoid dependencies and you need to map a mixed dictionaries/iterables collection with any combination of nesting and deepness, you can use the following solution:

```
def map_nested_coll(func,obj):
    if '__iter__' in dir(obj) and type(obj) not in (str,bytes):
        if type(obj) == dict:
            return {k:map_nested_coll(func,v) for k,v in obj.items()}
        else:
            return tuple(map_nested_coll(func,x) for x in obj)
    else:
        return func(obj)

```

In order to retain simplicity, non-dict iterables are converted to tuples (you can convert to `list` instead of `tuple` if you like, but converting to tuples is slightly faster). Also, although strings and bytes are iterables, usually you want to apply `func` on the whole string or bytes, so they are filtered out and not treated like iterables.

The advantage of this solution is that it works with any kind of iterable (even generators like `zip`, `range` and `map`) and handles edge cases well (see below):

```
>>> func = lambda x: x/2
>>> map_nested_coll(func, dict(a=1,b=dict(c=2,d=[3,(41,42),5]),e=[6,7]))
{'a': 0.5, 'b': {'c': 1.0, 'd': (1.5, (20.5, 21.0), 2.5)}, 'e': (3.0, 3.5)}
>>> map_nested_coll(func, [1,dict(a=2,b=3),(4,5)])
(0.5, {'a': 1.0, 'b': 1.5}, (2.0, 2.5))
>>> map_nested_itr(func, map(lambda x: 1+x, range(3)))
(0.5, 1.0, 1.5)
>>> map_nested_coll(func, 9)
4.5
>>> map_nested_coll(func, [])
()
>>> map_nested_itr(func, dict())
{}

```