map-like function on dict values?

Raymond Hettinger python at rcn.com
Sat Mar 2 03:46:52 EST 2002


"Skip Montanaro" <skip at pobox.com> wrote in message
news:mailman.1014929989.16553.python-list at python.org...
>
> def mapvalue(f, d):
>         keys = d.keys()
>         map(operator.setitem, [d]*len(keys), keys, map(f, d.values()))
>


The mapvalue test needs a tune-up.
Skipping the operator module and calling setitem
directly speeds things up a bit:

def tunedmap(f, d):
        return map( d.__setitem__, d.keys(), map(f, d.values()) )

I re-timed the result and it was close to the same as achieved for
iteriteritems.


Raymond Hettinger





More information about the Python-list mailing list