[Tutor] Performance difference, ``in'' vs ``has_key()''

Kent Johnson kent37 at tds.net
Mon Jul 18 03:47:05 CEST 2005

Bill Campbell wrote:
> I'm going to be doing some work where I'll be doing existence
> testings on keys on about a million records where it may require
> multiple passes so I'm a bit concerned about the timing of these
> tests.
> Is there any significant performance difference between the
> tests, ``key in dictionary'' and ``dictionary.has_key(key)''?
> I would prefer using the ``key in'' because it's a bit easier to
> type, and can also be used with lists in addition to dictionaries.

Are you sure you have a problem? How long does it take to process 1000 records?

The best way to answer these questions is with the timeit module running production code on production data. Second best is to use timeit on simple test cases. Third best (a distant contender) is to guess and ask other people their opinion.

But since I am feeling lazy and not in a coding mood I will give you my opinion instead of showing you how to use timeit =:-0

I think 'key in dict' is faster than 'dict.has_key()' because it avoids the attribute lookup for has_key(). You can see that in Alan Munroe's post. Though if you are looking up keys in a loop you can hoist the lookup out of the loop with something like
d = {}
hk = d.has_key
for i in someList:
  if hk(i):
    # do something if i is in d

If you can write the code to use dict indexing and exception handling that may be faster than checking for the key, depending on how often the exception is raised.

Finally, if you post a code snippet of what you are trying to do on comp.lang.python you will likely get many well-considered responses. The real experts in optimization hang out there.


> A related question is where's the trade-off between using ``in''
> with a list, and a dictionary?  I presume that using it with
> small hashes will be faster than dictionries since it doesn't
> have to calculate the hashes.
> Bill
> --
> INTERNET:   bill at Celestial.COM  Bill Campbell; Celestial Software LLC
> UUCP:               camco!bill  PO Box 820; 6641 E. Mercer Way
> FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676
> URL: http://www.celestial.com/
> Government is the great fiction, through which everbody endeavors to
> live at the expense of everybody else.  -- Frederic Bastiat
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list