Back
I wonder what is better to do:
d = {'a': 1, 'b': 2} 'a' in d Trueor:d = {'a': 1, 'b': 2}d.has_key('a') True
d = {'a': 1, 'b': 2}
'a' in d
True
or:
d.has_key('a')
If we talk about a more Pythonic way then “in” is preferred over has_key(). The advantage of “in” is it will work on any collection.
has_key() is removed in Python 3.x. It is only in Python 2.x which is used very less. Also, it is a dictionary method and only used with dictionaries.
Learn more about Python from an expert. Enroll in our Python Course
31k questions
32.8k answers
501 comments
693 users