HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonCriticalCanonical

Should I use 'has_key()' or 'in' on Python dicts?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
shoulddictsusehas_keypython

Problem

Given:

>>> 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')
True

Solution

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.