def update_in(d, keys, func, default=None, factory=dict):
    assert len(keys) > 0
    k, ks = keys[0], keys[1:]
    if ks:
        return assoc(d, k, update_in(d[k] if (k in d) else factory(),
                                     ks, func, default, factory),
                     factory)
    else:
        innermost = func(d[k]) if (k in d) else func(default)
        return assoc(d, k, innermost, factory)
Update value in a (potentially) nested dictionary
dataset/ETHPy150Open pytoolz/toolz/toolz/dicttoolz.py/update_in