def varmap(func, var, context=None, name=None):
 if (context is None):
  context = {}
 objid = id(var)
 if (objid in context):
  return func(name, '<...>')
 context[objid] = 1
 if isinstance(var, dict):
  ret = dict(((k, varmap(func, v, context, k)) for (k, v) in iteritems(var)))
 elif isinstance(var, (list, tuple)):
  ret = [varmap(func, f, context, name) for f in var]
 else:
  ret = func(name, var)
 del context[objid]
 return ret

def varmap ( func , var , context = None , name = None ) : if ( context is None ) : context = { } objid = id ( var ) if ( objid in context ) : return func ( name , '<...>' ) context [ objid ] = 1 if isinstance ( var , dict ) : ret = dict ( ( ( k , varmap ( func , v , context , k ) ) for ( k , v ) in iteritems ( var ) ) ) elif isinstance ( var , ( list , tuple ) ) : ret = [ varmap ( func , f , context , name ) for f in var ] else : ret = func ( name , var ) del context [ objid ] return ret
executes func on all values recurisively discovering dict and list scoped values .