1121

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
0

9 Answers 9

1617

in is definitely more pythonic.

In fact has_key() was removed in Python 3.x.

8
  • 3
    As an addition, in Python 3, to check for the existence in values, instead of the keys, try >>> 1 in d.values()
    – riza
    Aug 24, 2009 at 18:12
  • 275
    One semi-gotcha to avoid though is to make sure you do: "key in some_dict" rather than "key in some_dict.keys()". Both are equivalent semantically, but performance-wise the latter is much slower (O(n) vs O(1)). I've seen people do the "in dict.keys()" thinking it's more explicit & therefore better. Nov 9, 2011 at 20:55
  • 4
    @AdamParkin I demonstrated your comment in my answer stackoverflow.com/a/41390975/117471 Dec 30, 2016 at 5:17
  • 13
    @AdamParkin In Python 3, keys() is just a set-like view into a dictionary rather than a copy, so x in d.keys() is O(1). Still, x in d is more Pythonic. Aug 1, 2018 at 8:48
  • 3
    @AdamParkin Interesting, I didn't see that. I suppose it's because x in d.keys() must construct and destroy a temporary object, complete with the memory allocation that entails, where x in d.keys() is just doing an arithmetic operation (computing the hash) and doing a lookup. Note that d.keys() is only about 10 times as long as this, which is still not long really. I haven't checked but I'm still pretty sure it's only O(1). Aug 2, 2018 at 8:40
274

in wins hands-down, not just in elegance (and not being deprecated;-) but also in performance, e.g.:

$ python -mtimeit -s'd=dict.fromkeys(range(99))' '12 in d'
10000000 loops, best of 3: 0.0983 usec per loop
$ python -mtimeit -s'd=dict.fromkeys(range(99))' 'd.has_key(12)'
1000000 loops, best of 3: 0.21 usec per loop

While the following observation is not always true, you'll notice that usually, in Python, the faster solution is more elegant and Pythonic; that's why -mtimeit is SO helpful -- it's not just about saving a hundred nanoseconds here and there!-)

2
  • 4
    Thanks for this, made verifying that "in some_dict" is in fact O(1) much easier (try increasing the 99 to say 1999, and you'll find the runtime is about the same). Nov 9, 2011 at 21:00
  • 4
    has_key appears to be O(1) too.
    – dan-gph
    Jan 6, 2015 at 4:11
118

According to python docs:

has_key() is deprecated in favor of key in d.

1
  • 8
    has_key() is now removed in Python 3 Nov 14, 2019 at 15:21
48

Use dict.has_key() if (and only if) your code is required to be runnable by Python versions earlier than 2.3 (when key in dict was introduced).

2
  • 5
    The WebSphere update in 2013 uses Jython 2.1 as its main scripting language. So this is unfortunately still a useful thing to note, five years after you noted it. Sep 24, 2014 at 11:49
  • Great answer! To get a more pythonic check in Python versions < 2.3 (in my case Jython 2.1 as well) you can use key in dict.keys(). Dec 7, 2023 at 12:33
27

There is one example where in actually kills your performance.

If you use in on a O(1) container that only implements __getitem__ and has_key() but not __contains__ you will turn an O(1) search into an O(N) search (as in falls back to a linear search via __getitem__).

Fix is obviously trivial:

def __contains__(self, x):
    return self.has_key(x)
2
  • 9
    This answer was applicable when it was posted, but 99.95% of readers can safely ignore it. In most cases, if you're working with something this obscure you'll know it.
    – wizzwizz4
    Jul 27, 2018 at 13:17
  • 4
    This really is not an issue. has_key() is specific to Python 2 dictionaries. in / __contains__ is the correct API to use; for those containers where a full scan is unavoidable there is no has_key() method anyway, and if there is a O(1) approach then that'll be use-case specific and so up to the developer to pick the right data type for the problem.
    – Martijn Pieters
    Jan 5, 2019 at 19:25
26

Solution to dict.has_key() is deprecated, use 'in' -- sublime text editor 3

Here I have taken an example of dictionary named 'ages' -

ages = {}

# Add a couple of names to the dictionary
ages['Sue'] = 23

ages['Peter'] = 19

ages['Andrew'] = 78

ages['Karren'] = 45

# use of 'in' in if condition instead of function_name.has_key(key-name).
if 'Sue' in ages:

    print "Sue is in the dictionary. She is", ages['Sue'], "years old"

else:

    print "Sue is not in the dictionary"
2
  • 7
    Correct, but it was already answered, welcome to Stackoveflow, thanks for the example, always check the answers though!
    – igorgue
    Feb 23, 2016 at 19:51
  • 1
    @igorgue im not sure about the downvotes to her. Her answer might be similar to the ones already answered, but she provides an example. Isnt that worthy enough to be an answer of SO? May 22, 2016 at 13:34
21

Expanding on Alex Martelli's performance tests with Adam Parkin's comments...

$ python3.5 -mtimeit -s'd=dict.fromkeys(range( 99))' 'd.has_key(12)'
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/timeit.py", line 301, in main
    x = t.timeit(number)
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/timeit.py", line 178, in timeit
    timing = self.inner(it, self.timer)
  File "<timeit-src>", line 6, in inner
    d.has_key(12)
AttributeError: 'dict' object has no attribute 'has_key'

$ python2.7 -mtimeit -s'd=dict.fromkeys(range(  99))' 'd.has_key(12)'
10000000 loops, best of 3: 0.0872 usec per loop

$ python2.7 -mtimeit -s'd=dict.fromkeys(range(1999))' 'd.has_key(12)'
10000000 loops, best of 3: 0.0858 usec per loop

$ python3.5 -mtimeit -s'd=dict.fromkeys(range(  99))' '12 in d'
10000000 loops, best of 3: 0.031 usec per loop

$ python3.5 -mtimeit -s'd=dict.fromkeys(range(1999))' '12 in d'
10000000 loops, best of 3: 0.033 usec per loop

$ python3.5 -mtimeit -s'd=dict.fromkeys(range(  99))' '12 in d.keys()'
10000000 loops, best of 3: 0.115 usec per loop

$ python3.5 -mtimeit -s'd=dict.fromkeys(range(1999))' '12 in d.keys()'
10000000 loops, best of 3: 0.117 usec per loop
4
  • Wonderful statistics, sometimes implicit might be better than explicit (at least in efficiency)...
    – varun
    Mar 30, 2018 at 5:06
  • Thank you, @varun. I had forgotten about this answer. I need to do this kind of testing more often. I regularly read long threads where people argue about The Best Way™ to do things. But I rarely remember how easy this was to get proof. Mar 30, 2018 at 14:47
  • this experiment has a defect, it mixed the dict creation time with the key searching time. it is better to separate the two to measure the time spent on key searching only. once you separate the two, the timing result would show that both 'key in D' and 'key in D.keys()' appear to be O(1). No essential difference, although key in D.keys() is a bit slower than key in D, it is not O(N) vs O(1). Sep 3, 2020 at 22:56
  • i used python3, so the conclusion i had was for python3 (in python2 likely it is O(N) vs O(1)), but i did not see this in python3. Sep 3, 2020 at 23:54
15

has_key is a dictionary method, but in will work on any collection, and even when __contains__ is missing, in will use any other method to iterate the collection to find out.

10
  • 1
    And does also work on iterators "x in xrange(90, 200) <=> 90 <= x < 200" Aug 28, 2009 at 13:21
  • 1
    …: This looks like a very bad idea: 50 operations instead of 2.
    – Clément
    Sep 22, 2016 at 22:12
  • 1
    @Clément In Python 3, it's actually quite efficient to do in tests on range objects. I'm not so sure about its efficiency on Python 2 xrange, though. ;)
    – PM 2Ring
    Nov 29, 2018 at 18:00
  • 1
    @Clément not in Python 3; __contains__ can trivially calculate if a value is in the range or not.
    – Martijn Pieters
    Jan 5, 2019 at 19:21
  • 1
    @AlexandreHuat Your timing includes the overhead of creating a new range instance each time. Using a single, pre-existing instance the "integer in range" test is about 40% faster in my timings. Feb 10, 2020 at 15:54
-6

If you have something like this:

t.has_key(ew)

change it to below for running on Python 3.X and above:

key = ew
if key not in t
1
  • 11
    No, you inverted the test. t.has_key(ew) returns True if the value ew references is also a key in the dictionary. key not in t returns True if the value is not in the dictionary. Moreover, the key = ew alias is very, very redundant. The correct spelling is if ew in t. Which is what the accepted answer from 8 years prior already told you.
    – Martijn Pieters
    Jan 5, 2019 at 19:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.