If Dict Contains a particular key
mensanator at aol.com
mensanator at aol.com
Thu Apr 26 19:27:29 EDT 2007
On Apr 26, 12:47 am, a... at mac.com (Alex Martelli) wrote:
> mensana... at aol.com <mensana... at aol.com> wrote:
>
> ...
>
> > > if 'a' in thedict:
> > > ...
>
> > > There's no need for the call to keys().
>
> > Why not
>
> > if thedict.has_key('a'):
> > pass
> > elde:
> > pass
>
> has_key exists only for backwards compatibility; the 'in' operator is
> preferable.
>
> $ python -mtimeit -s'd={}' 'd.has_key(23)'
> 1000000 loops, best of 3: 0.289 usec per loop
> $ python -mtimeit -s'd={}' '23 in d'
> 10000000 loops, best of 3: 0.139 usec per loop
>
> Why consume twice as much time with no advantages whatever?!
Did I wander into comp.lang.perl by mistake?
>
> Alex
More information about the Python-list
mailing list