0 votes
1 view
in Python by (33.2k 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 (107k 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 !


Categories

...