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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







797















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









share|improve this question































    797















    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









    share|improve this question



























      797












      797








      797


      94






      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









      share|improve this question
















      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






      python dictionary






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 29 '18 at 6:37









      smci

      15.6k679109




      15.6k679109










      asked Aug 24 '09 at 16:30









      igorgueigorgue

      7,58793250




      7,58793250
























          10 Answers
          10






          active

          oldest

          votes


















          1115














          in is definitely more pythonic.



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






          share|improve this answer





















          • 2





            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 '09 at 18:12






          • 184





            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.

            – Adam Parkin
            Nov 9 '11 at 20:55






          • 3





            in works with 2.6 too right?

            – Logan
            Jan 17 '13 at 4:07






          • 2





            @AdamParkin I demonstrated your comment in my answer stackoverflow.com/a/41390975/117471

            – Bruno Bronosky
            Dec 30 '16 at 5:17






          • 4





            @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.

            – Arthur Tacca
            Aug 1 '18 at 8:48



















          244














          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!-)






          share|improve this answer



















          • 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).

            – Adam Parkin
            Nov 9 '11 at 21:00






          • 2





            has_key appears to be O(1) too.

            – dan-gph
            Jan 6 '15 at 4:11



















          86














          According to python docs:




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







          share|improve this answer

































            39














            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).






            share|improve this answer





















            • 1





              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.

              – ArtOfWarfare
              Sep 24 '14 at 11:49



















            21














            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)





            share|improve this answer



















            • 5





              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 '18 at 13:17











            • How did you actually calculate the number 99.95%? Kidding...

              – Maruf Maniruzzaman
              Dec 9 '18 at 17:42






            • 1





              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 at 19:25



















            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.






            share|improve this answer



















            • 1





              And does also work on iterators "x in xrange(90, 200) <=> 90 <= x < 200"

              – u0b34a0f6ae
              Aug 28 '09 at 13:21






            • 1





              …: This looks like a very bad idea: 50 operations instead of 2.

              – Clément
              Sep 22 '16 at 22:12













            • @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 '18 at 18:00











            • @Clément not in Python 3; __contains__ can trivially calculate if a value is in the range or not.

              – Martijn Pieters
              Jan 5 at 19:21













            • @PM2Ring Not necessarily. Try 1.0 in range(10**2, 0, -1) and then try 1.0 in range(10**10, 0, -1)

              – wim
              Jan 5 at 23:15



















            14














            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"





            share|improve this answer





















            • 5





              Correct, but it was already answered, welcome to Stackoveflow, thanks for the example, always check the answers though!

              – igorgue
              Feb 23 '16 at 19:51











            • @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?

              – Akshat Agarwal
              May 22 '16 at 13:34






            • 1





              @AkshatAgarwal No: the question already had an example.

              – Clément
              Sep 22 '16 at 22:13



















            10














            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





            share|improve this answer


























            • Wonderful statistics, sometimes implicit might be better than explicit (at least in efficiency)...

              – varun
              Mar 30 '18 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.

              – Bruno Bronosky
              Mar 30 '18 at 14:47





















            9














            Python 2.x supports has_key().



            Python 2.3+ and Python 3.x support in.






            share|improve this answer





















            • 1





              Yes, and has_key is deprecated. There is nothing new in this answer over the top 4 answers posted 3 years prior.

              – Martijn Pieters
              Jan 5 at 19:20





















            1














            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





            share|improve this answer





















            • 3





              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 at 19:17










            protected by Community Jan 12 at 18:27



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?














            10 Answers
            10






            active

            oldest

            votes








            10 Answers
            10






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1115














            in is definitely more pythonic.



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






            share|improve this answer





















            • 2





              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 '09 at 18:12






            • 184





              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.

              – Adam Parkin
              Nov 9 '11 at 20:55






            • 3





              in works with 2.6 too right?

              – Logan
              Jan 17 '13 at 4:07






            • 2





              @AdamParkin I demonstrated your comment in my answer stackoverflow.com/a/41390975/117471

              – Bruno Bronosky
              Dec 30 '16 at 5:17






            • 4





              @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.

              – Arthur Tacca
              Aug 1 '18 at 8:48
















            1115














            in is definitely more pythonic.



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






            share|improve this answer





















            • 2





              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 '09 at 18:12






            • 184





              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.

              – Adam Parkin
              Nov 9 '11 at 20:55






            • 3





              in works with 2.6 too right?

              – Logan
              Jan 17 '13 at 4:07






            • 2





              @AdamParkin I demonstrated your comment in my answer stackoverflow.com/a/41390975/117471

              – Bruno Bronosky
              Dec 30 '16 at 5:17






            • 4





              @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.

              – Arthur Tacca
              Aug 1 '18 at 8:48














            1115












            1115








            1115







            in is definitely more pythonic.



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






            share|improve this answer















            in is definitely more pythonic.



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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 30 '13 at 15:56









            bluish

            14.3k1694149




            14.3k1694149










            answered Aug 24 '09 at 16:33









            tonfatonfa

            18.7k23037




            18.7k23037








            • 2





              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 '09 at 18:12






            • 184





              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.

              – Adam Parkin
              Nov 9 '11 at 20:55






            • 3





              in works with 2.6 too right?

              – Logan
              Jan 17 '13 at 4:07






            • 2





              @AdamParkin I demonstrated your comment in my answer stackoverflow.com/a/41390975/117471

              – Bruno Bronosky
              Dec 30 '16 at 5:17






            • 4





              @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.

              – Arthur Tacca
              Aug 1 '18 at 8:48














            • 2





              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 '09 at 18:12






            • 184





              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.

              – Adam Parkin
              Nov 9 '11 at 20:55






            • 3





              in works with 2.6 too right?

              – Logan
              Jan 17 '13 at 4:07






            • 2





              @AdamParkin I demonstrated your comment in my answer stackoverflow.com/a/41390975/117471

              – Bruno Bronosky
              Dec 30 '16 at 5:17






            • 4





              @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.

              – Arthur Tacca
              Aug 1 '18 at 8:48








            2




            2





            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 '09 at 18:12





            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 '09 at 18:12




            184




            184





            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.

            – Adam Parkin
            Nov 9 '11 at 20:55





            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.

            – Adam Parkin
            Nov 9 '11 at 20:55




            3




            3





            in works with 2.6 too right?

            – Logan
            Jan 17 '13 at 4:07





            in works with 2.6 too right?

            – Logan
            Jan 17 '13 at 4:07




            2




            2





            @AdamParkin I demonstrated your comment in my answer stackoverflow.com/a/41390975/117471

            – Bruno Bronosky
            Dec 30 '16 at 5:17





            @AdamParkin I demonstrated your comment in my answer stackoverflow.com/a/41390975/117471

            – Bruno Bronosky
            Dec 30 '16 at 5:17




            4




            4





            @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.

            – Arthur Tacca
            Aug 1 '18 at 8:48





            @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.

            – Arthur Tacca
            Aug 1 '18 at 8:48













            244














            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!-)






            share|improve this answer



















            • 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).

              – Adam Parkin
              Nov 9 '11 at 21:00






            • 2





              has_key appears to be O(1) too.

              – dan-gph
              Jan 6 '15 at 4:11
















            244














            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!-)






            share|improve this answer



















            • 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).

              – Adam Parkin
              Nov 9 '11 at 21:00






            • 2





              has_key appears to be O(1) too.

              – dan-gph
              Jan 6 '15 at 4:11














            244












            244








            244







            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!-)






            share|improve this answer













            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!-)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 24 '09 at 18:12









            Alex MartelliAlex Martelli

            635k12910461284




            635k12910461284








            • 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).

              – Adam Parkin
              Nov 9 '11 at 21:00






            • 2





              has_key appears to be O(1) too.

              – dan-gph
              Jan 6 '15 at 4:11














            • 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).

              – Adam Parkin
              Nov 9 '11 at 21:00






            • 2





              has_key appears to be O(1) too.

              – dan-gph
              Jan 6 '15 at 4:11








            4




            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).

            – Adam Parkin
            Nov 9 '11 at 21:00





            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).

            – Adam Parkin
            Nov 9 '11 at 21:00




            2




            2





            has_key appears to be O(1) too.

            – dan-gph
            Jan 6 '15 at 4:11





            has_key appears to be O(1) too.

            – dan-gph
            Jan 6 '15 at 4:11











            86














            According to python docs:




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







            share|improve this answer






























              86














              According to python docs:




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







              share|improve this answer




























                86












                86








                86







                According to python docs:




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







                share|improve this answer















                According to python docs:




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








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 11 '13 at 6:27









                jamylak

                85.6k18181199




                85.6k18181199










                answered Aug 24 '09 at 16:33









                Nadia AlramliNadia Alramli

                80.8k25153147




                80.8k25153147























                    39














                    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).






                    share|improve this answer





















                    • 1





                      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.

                      – ArtOfWarfare
                      Sep 24 '14 at 11:49
















                    39














                    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).






                    share|improve this answer





















                    • 1





                      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.

                      – ArtOfWarfare
                      Sep 24 '14 at 11:49














                    39












                    39








                    39







                    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).






                    share|improve this answer















                    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).







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 18 '12 at 16:52









                    Mike Samuel

                    94.6k23174215




                    94.6k23174215










                    answered Aug 24 '09 at 22:11









                    John MachinJohn Machin

                    65.7k7101162




                    65.7k7101162








                    • 1





                      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.

                      – ArtOfWarfare
                      Sep 24 '14 at 11:49














                    • 1





                      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.

                      – ArtOfWarfare
                      Sep 24 '14 at 11:49








                    1




                    1





                    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.

                    – ArtOfWarfare
                    Sep 24 '14 at 11:49





                    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.

                    – ArtOfWarfare
                    Sep 24 '14 at 11:49











                    21














                    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)





                    share|improve this answer



















                    • 5





                      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 '18 at 13:17











                    • How did you actually calculate the number 99.95%? Kidding...

                      – Maruf Maniruzzaman
                      Dec 9 '18 at 17:42






                    • 1





                      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 at 19:25
















                    21














                    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)





                    share|improve this answer



















                    • 5





                      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 '18 at 13:17











                    • How did you actually calculate the number 99.95%? Kidding...

                      – Maruf Maniruzzaman
                      Dec 9 '18 at 17:42






                    • 1





                      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 at 19:25














                    21












                    21








                    21







                    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)





                    share|improve this answer













                    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)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 18 '12 at 16:45









                    schlenkschlenk

                    5,83911524




                    5,83911524








                    • 5





                      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 '18 at 13:17











                    • How did you actually calculate the number 99.95%? Kidding...

                      – Maruf Maniruzzaman
                      Dec 9 '18 at 17:42






                    • 1





                      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 at 19:25














                    • 5





                      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 '18 at 13:17











                    • How did you actually calculate the number 99.95%? Kidding...

                      – Maruf Maniruzzaman
                      Dec 9 '18 at 17:42






                    • 1





                      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 at 19:25








                    5




                    5





                    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 '18 at 13:17





                    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 '18 at 13:17













                    How did you actually calculate the number 99.95%? Kidding...

                    – Maruf Maniruzzaman
                    Dec 9 '18 at 17:42





                    How did you actually calculate the number 99.95%? Kidding...

                    – Maruf Maniruzzaman
                    Dec 9 '18 at 17:42




                    1




                    1





                    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 at 19:25





                    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 at 19:25











                    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.






                    share|improve this answer



















                    • 1





                      And does also work on iterators "x in xrange(90, 200) <=> 90 <= x < 200"

                      – u0b34a0f6ae
                      Aug 28 '09 at 13:21






                    • 1





                      …: This looks like a very bad idea: 50 operations instead of 2.

                      – Clément
                      Sep 22 '16 at 22:12













                    • @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 '18 at 18:00











                    • @Clément not in Python 3; __contains__ can trivially calculate if a value is in the range or not.

                      – Martijn Pieters
                      Jan 5 at 19:21













                    • @PM2Ring Not necessarily. Try 1.0 in range(10**2, 0, -1) and then try 1.0 in range(10**10, 0, -1)

                      – wim
                      Jan 5 at 23:15
















                    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.






                    share|improve this answer



















                    • 1





                      And does also work on iterators "x in xrange(90, 200) <=> 90 <= x < 200"

                      – u0b34a0f6ae
                      Aug 28 '09 at 13:21






                    • 1





                      …: This looks like a very bad idea: 50 operations instead of 2.

                      – Clément
                      Sep 22 '16 at 22:12













                    • @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 '18 at 18:00











                    • @Clément not in Python 3; __contains__ can trivially calculate if a value is in the range or not.

                      – Martijn Pieters
                      Jan 5 at 19:21













                    • @PM2Ring Not necessarily. Try 1.0 in range(10**2, 0, -1) and then try 1.0 in range(10**10, 0, -1)

                      – wim
                      Jan 5 at 23:15














                    15












                    15








                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 24 '09 at 18:35









                    u0b34a0f6aeu0b34a0f6ae

                    33.2k117796




                    33.2k117796








                    • 1





                      And does also work on iterators "x in xrange(90, 200) <=> 90 <= x < 200"

                      – u0b34a0f6ae
                      Aug 28 '09 at 13:21






                    • 1





                      …: This looks like a very bad idea: 50 operations instead of 2.

                      – Clément
                      Sep 22 '16 at 22:12













                    • @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 '18 at 18:00











                    • @Clément not in Python 3; __contains__ can trivially calculate if a value is in the range or not.

                      – Martijn Pieters
                      Jan 5 at 19:21













                    • @PM2Ring Not necessarily. Try 1.0 in range(10**2, 0, -1) and then try 1.0 in range(10**10, 0, -1)

                      – wim
                      Jan 5 at 23:15














                    • 1





                      And does also work on iterators "x in xrange(90, 200) <=> 90 <= x < 200"

                      – u0b34a0f6ae
                      Aug 28 '09 at 13:21






                    • 1





                      …: This looks like a very bad idea: 50 operations instead of 2.

                      – Clément
                      Sep 22 '16 at 22:12













                    • @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 '18 at 18:00











                    • @Clément not in Python 3; __contains__ can trivially calculate if a value is in the range or not.

                      – Martijn Pieters
                      Jan 5 at 19:21













                    • @PM2Ring Not necessarily. Try 1.0 in range(10**2, 0, -1) and then try 1.0 in range(10**10, 0, -1)

                      – wim
                      Jan 5 at 23:15








                    1




                    1





                    And does also work on iterators "x in xrange(90, 200) <=> 90 <= x < 200"

                    – u0b34a0f6ae
                    Aug 28 '09 at 13:21





                    And does also work on iterators "x in xrange(90, 200) <=> 90 <= x < 200"

                    – u0b34a0f6ae
                    Aug 28 '09 at 13:21




                    1




                    1





                    …: This looks like a very bad idea: 50 operations instead of 2.

                    – Clément
                    Sep 22 '16 at 22:12







                    …: This looks like a very bad idea: 50 operations instead of 2.

                    – Clément
                    Sep 22 '16 at 22:12















                    @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 '18 at 18:00





                    @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 '18 at 18:00













                    @Clément not in Python 3; __contains__ can trivially calculate if a value is in the range or not.

                    – Martijn Pieters
                    Jan 5 at 19:21







                    @Clément not in Python 3; __contains__ can trivially calculate if a value is in the range or not.

                    – Martijn Pieters
                    Jan 5 at 19:21















                    @PM2Ring Not necessarily. Try 1.0 in range(10**2, 0, -1) and then try 1.0 in range(10**10, 0, -1)

                    – wim
                    Jan 5 at 23:15





                    @PM2Ring Not necessarily. Try 1.0 in range(10**2, 0, -1) and then try 1.0 in range(10**10, 0, -1)

                    – wim
                    Jan 5 at 23:15











                    14














                    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"





                    share|improve this answer





















                    • 5





                      Correct, but it was already answered, welcome to Stackoveflow, thanks for the example, always check the answers though!

                      – igorgue
                      Feb 23 '16 at 19:51











                    • @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?

                      – Akshat Agarwal
                      May 22 '16 at 13:34






                    • 1





                      @AkshatAgarwal No: the question already had an example.

                      – Clément
                      Sep 22 '16 at 22:13
















                    14














                    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"





                    share|improve this answer





















                    • 5





                      Correct, but it was already answered, welcome to Stackoveflow, thanks for the example, always check the answers though!

                      – igorgue
                      Feb 23 '16 at 19:51











                    • @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?

                      – Akshat Agarwal
                      May 22 '16 at 13:34






                    • 1





                      @AkshatAgarwal No: the question already had an example.

                      – Clément
                      Sep 22 '16 at 22:13














                    14












                    14








                    14







                    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"





                    share|improve this answer















                    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"






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 11 '16 at 19:46







                    user5547025

















                    answered Feb 23 '16 at 10:29









                    Greena modiGreena modi

                    30322




                    30322








                    • 5





                      Correct, but it was already answered, welcome to Stackoveflow, thanks for the example, always check the answers though!

                      – igorgue
                      Feb 23 '16 at 19:51











                    • @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?

                      – Akshat Agarwal
                      May 22 '16 at 13:34






                    • 1





                      @AkshatAgarwal No: the question already had an example.

                      – Clément
                      Sep 22 '16 at 22:13














                    • 5





                      Correct, but it was already answered, welcome to Stackoveflow, thanks for the example, always check the answers though!

                      – igorgue
                      Feb 23 '16 at 19:51











                    • @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?

                      – Akshat Agarwal
                      May 22 '16 at 13:34






                    • 1





                      @AkshatAgarwal No: the question already had an example.

                      – Clément
                      Sep 22 '16 at 22:13








                    5




                    5





                    Correct, but it was already answered, welcome to Stackoveflow, thanks for the example, always check the answers though!

                    – igorgue
                    Feb 23 '16 at 19:51





                    Correct, but it was already answered, welcome to Stackoveflow, thanks for the example, always check the answers though!

                    – igorgue
                    Feb 23 '16 at 19:51













                    @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?

                    – Akshat Agarwal
                    May 22 '16 at 13:34





                    @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?

                    – Akshat Agarwal
                    May 22 '16 at 13:34




                    1




                    1





                    @AkshatAgarwal No: the question already had an example.

                    – Clément
                    Sep 22 '16 at 22:13





                    @AkshatAgarwal No: the question already had an example.

                    – Clément
                    Sep 22 '16 at 22:13











                    10














                    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





                    share|improve this answer


























                    • Wonderful statistics, sometimes implicit might be better than explicit (at least in efficiency)...

                      – varun
                      Mar 30 '18 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.

                      – Bruno Bronosky
                      Mar 30 '18 at 14:47


















                    10














                    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





                    share|improve this answer


























                    • Wonderful statistics, sometimes implicit might be better than explicit (at least in efficiency)...

                      – varun
                      Mar 30 '18 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.

                      – Bruno Bronosky
                      Mar 30 '18 at 14:47
















                    10












                    10








                    10







                    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





                    share|improve this answer















                    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






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 31 '17 at 16:11

























                    answered Dec 30 '16 at 5:16









                    Bruno BronoskyBruno Bronosky

                    36.1k58387




                    36.1k58387













                    • Wonderful statistics, sometimes implicit might be better than explicit (at least in efficiency)...

                      – varun
                      Mar 30 '18 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.

                      – Bruno Bronosky
                      Mar 30 '18 at 14:47





















                    • Wonderful statistics, sometimes implicit might be better than explicit (at least in efficiency)...

                      – varun
                      Mar 30 '18 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.

                      – Bruno Bronosky
                      Mar 30 '18 at 14:47



















                    Wonderful statistics, sometimes implicit might be better than explicit (at least in efficiency)...

                    – varun
                    Mar 30 '18 at 5:06





                    Wonderful statistics, sometimes implicit might be better than explicit (at least in efficiency)...

                    – varun
                    Mar 30 '18 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.

                    – Bruno Bronosky
                    Mar 30 '18 at 14:47







                    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.

                    – Bruno Bronosky
                    Mar 30 '18 at 14:47













                    9














                    Python 2.x supports has_key().



                    Python 2.3+ and Python 3.x support in.






                    share|improve this answer





















                    • 1





                      Yes, and has_key is deprecated. There is nothing new in this answer over the top 4 answers posted 3 years prior.

                      – Martijn Pieters
                      Jan 5 at 19:20


















                    9














                    Python 2.x supports has_key().



                    Python 2.3+ and Python 3.x support in.






                    share|improve this answer





















                    • 1





                      Yes, and has_key is deprecated. There is nothing new in this answer over the top 4 answers posted 3 years prior.

                      – Martijn Pieters
                      Jan 5 at 19:20
















                    9












                    9








                    9







                    Python 2.x supports has_key().



                    Python 2.3+ and Python 3.x support in.






                    share|improve this answer















                    Python 2.x supports has_key().



                    Python 2.3+ and Python 3.x support in.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 24 '14 at 11:51









                    ArtOfWarfare

                    12.9k887138




                    12.9k887138










                    answered May 9 '12 at 8:06









                    SiriusKoderSiriusKoder

                    3002412




                    3002412








                    • 1





                      Yes, and has_key is deprecated. There is nothing new in this answer over the top 4 answers posted 3 years prior.

                      – Martijn Pieters
                      Jan 5 at 19:20
















                    • 1





                      Yes, and has_key is deprecated. There is nothing new in this answer over the top 4 answers posted 3 years prior.

                      – Martijn Pieters
                      Jan 5 at 19:20










                    1




                    1





                    Yes, and has_key is deprecated. There is nothing new in this answer over the top 4 answers posted 3 years prior.

                    – Martijn Pieters
                    Jan 5 at 19:20







                    Yes, and has_key is deprecated. There is nothing new in this answer over the top 4 answers posted 3 years prior.

                    – Martijn Pieters
                    Jan 5 at 19:20













                    1














                    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





                    share|improve this answer





















                    • 3





                      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 at 19:17
















                    1














                    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





                    share|improve this answer





















                    • 3





                      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 at 19:17














                    1












                    1








                    1







                    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





                    share|improve this answer















                    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






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 2 '17 at 9:16









                    Giordano

                    2,86421635




                    2,86421635










                    answered Jan 24 '17 at 0:21









                    Harshita JhavarHarshita Jhavar

                    1107




                    1107








                    • 3





                      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 at 19:17














                    • 3





                      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 at 19:17








                    3




                    3





                    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 at 19:17





                    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 at 19:17





                    protected by Community Jan 12 at 18:27



                    Thank you for your interest in this question.
                    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                    Would you like to answer one of these unanswered questions instead?



                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    Idjassú

                    count number of partitions of a set with n elements into k subsets