Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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)
  • 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

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...