Given:
JavaScript
x
2
1
>>> d = {'a': 1, 'b': 2}
2
Which of the following is the best way to check if 'a'
is in d
?
JavaScript
1
3
1
>>> 'a' in d
2
True
3
JavaScript
1
3
1
>>> d.has_key('a')
2
True
3
Advertisement
Answer
in
is definitely more pythonic.
In fact has_key()
was removed in Python 3.x.