Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (33.1k points)

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

1 Answer

0 votes
by (106k points)
edited by
  • 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.

image

Learn more about Python from an expert. Enroll in our Python Course

Browse Categories

...