dict.has_key(x) versus 'x in dict'

Paul Melis paul at floorball-flamingos.nl
Wed Dec 6 07:17:53 EST 2006


Hello,

I've always been using the has_key() method to test if a dictionary
contains a certain key. Recently I tried the same using 'in', e.g.

d = { ... }
if k in d:
    ...

and found that it seems to perform a lot better when lots of key-tests
are to be performed. I also noticed that has_key() is scheduled to be
removed from future (C)Python versions.

Does the 'in' way of testing have an optimized implementation compared
to has_key()? Is that the reason has_key() is being phased out?

Thanks,
Paul



More information about the Python-list mailing list