Given:

>>> d = {'a': 1, 'b': 2}

Which of the following is the best way to check if 'a' is in d?

>>> 'a' in d
True
>>> d.has_key('a')
True

9 s
9

in is definitely more pythonic.

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

Leave a Reply

Your email address will not be published. Required fields are marked *