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)

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 nested dictionary inputs: d - dictionary on which to operate keys - list or tuple giving the location of the value to be changed in d func - function to operate on that value if keys == [k0 .