Should I use 'has_key()' or 'in' on Python dicts?

igorgue picture igorgue · Aug 24, 2009 · Viewed 689.5k times · Source

I wonder what is better to do:

d = {'a': 1, 'b': 2}
'a' in d
True

or:

d = {'a': 1, 'b': 2}
d.has_key('a')
True

Answer

tonfa picture tonfa · Aug 24, 2009

in is definitely more pythonic.

In fact has_key() was removed in Python 3.x.