[Python-3000] Iterators for dict keys, values, and items == annoying :)

Alex Martelli aleaxit at gmail.com
Thu Mar 23 22:21:27 CET 2006


On 3/23/06, skip at pobox.com <skip at pobox.com> wrote:
   ...
> Agreed.  I think there's also a perceived performance difference, whether
> one exists or not.  I'd be real surprised if
>
>     for key, val in somedict.iteritems():
>         blah(key, val)
>
> was faster than
>
>     for key, val in somedict.items():
>         blah(key, val)
>
> for small dicts.  Still, around work I see a great preference for the longer

Not sure what's a "small dict" in your world -- here, for example:

python2.4 -mtimeit -s'd=dict.fromkeys(range(23))' 'for k, v in
d.iteritems(): pass'
100000 loops, best of 3: 2.81 usec per loop

python2.4 -mtimeit -s'd=dict.fromkeys(range(23))' 'for k, v in d.items(): pass'
100000 loops, best of 3: 4.82 usec per loop

and an avoidable overhead of 2.01/2.81 = 71.5% does matter.  But maybe
you mean dictionaries that are much smaller than a couple dozen items?
Using range(3) instead of range(23), I measure 0.804 vs 1.36
microseconds -- still, even that 68% avoidable overhead, while
definitely less than 71.5%, is still pretty unpleasant, no?


Alex


More information about the Python-3000 mailing list