Has_key() in? Which one should I use?

Asked 2 years ago, Updated 2 years ago, 15 views

Which is the better way to use it? What's the difference between the two?

d = {'a': 1, 'b': 2}
"a" in #This?
d.has_key('a') #this?

python

2022-09-21 17:00

1 Answers

According to has_key(key), there are no cases where the two produce different results.

However, has_key() is not supported on Python 3 or higher Using in is more Python We recommend that you write in.


2022-09-21 17:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.