patternpythonCritical
Should I use 'has_key()' or 'in' on Python dicts?
Viewed 0 times
shouldpythonhas_keyusedicts
Problem
Given:
Which of the following is the best way to check if
>>> d = {'a': 1, 'b': 2}Which of the following is the best way to check if
'a' is in d?>>> 'a' in d
True>>> d.has_key('a')
TrueSolution
in is definitely more pythonic.In fact
has_key() was removed in Python 3.x.Context
Stack Overflow Q#1323410, score: 1638
Revisions (0)
No revisions yet.