Python – Should I use ‘has_key()’ or ‘in’ on Python dicts

dictionarypython

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

Best Answer

in is definitely more pythonic.

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