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;
}
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
add a comment |
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
add a comment |
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
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
python dictionary
edited Nov 29 '18 at 6:37
smci
15.6k679109
15.6k679109
asked Aug 24 '09 at 16:30
igorgueigorgue
7,58793250
7,58793250
add a comment |
add a comment |
10 Answers
10
active
oldest
votes
in
is definitely more pythonic.
In fact has_key()
was removed in Python 3.x.
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, sox in d.keys()
is O(1). Still,x in d
is more Pythonic.
– Arthur Tacca
Aug 1 '18 at 8:48
|
show 3 more comments
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!-)
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
add a comment |
According to python docs:
has_key()
is deprecated in favor of
key in d
.
add a comment |
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).
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
add a comment |
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)
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 nohas_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
add a comment |
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.
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 doin
tests onrange
objects. I'm not so sure about its efficiency on Python 2xrange
, 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. Try1.0 in range(10**2, 0, -1)
and then try1.0 in range(10**10, 0, -1)
– wim
Jan 5 at 23:15
|
show 2 more comments
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"
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
add a comment |
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
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
add a comment |
Python 2.x supports has_key()
.
Python 2.3+ and Python 3.x support in
.
1
Yes, andhas_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
add a comment |
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
3
No, you inverted the test.t.has_key(ew)
returnsTrue
if the valueew
references is also a key in the dictionary.key not in t
returnsTrue
if the value is not in the dictionary. Moreover, thekey = ew
alias is very, very redundant. The correct spelling isif ew in t
. Which is what the accepted answer from 8 years prior already told you.
– Martijn Pieters♦
Jan 5 at 19:17
add a comment |
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
in
is definitely more pythonic.
In fact has_key()
was removed in Python 3.x.
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, sox in d.keys()
is O(1). Still,x in d
is more Pythonic.
– Arthur Tacca
Aug 1 '18 at 8:48
|
show 3 more comments
in
is definitely more pythonic.
In fact has_key()
was removed in Python 3.x.
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, sox in d.keys()
is O(1). Still,x in d
is more Pythonic.
– Arthur Tacca
Aug 1 '18 at 8:48
|
show 3 more comments
in
is definitely more pythonic.
In fact has_key()
was removed in Python 3.x.
in
is definitely more pythonic.
In fact has_key()
was removed in Python 3.x.
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, sox in d.keys()
is O(1). Still,x in d
is more Pythonic.
– Arthur Tacca
Aug 1 '18 at 8:48
|
show 3 more comments
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, sox 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
|
show 3 more comments
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!-)
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
add a comment |
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!-)
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
add a comment |
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!-)
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!-)
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
add a comment |
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
add a comment |
According to python docs:
has_key()
is deprecated in favor of
key in d
.
add a comment |
According to python docs:
has_key()
is deprecated in favor of
key in d
.
add a comment |
According to python docs:
has_key()
is deprecated in favor of
key in d
.
According to python docs:
has_key()
is deprecated in favor of
key in d
.
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
add a comment |
add a comment |
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).
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
add a comment |
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).
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
add a comment |
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).
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).
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
add a comment |
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
add a comment |
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)
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 nohas_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
add a comment |
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)
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 nohas_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
add a comment |
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)
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)
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 nohas_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
add a comment |
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 nohas_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
add a comment |
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.
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 doin
tests onrange
objects. I'm not so sure about its efficiency on Python 2xrange
, 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. Try1.0 in range(10**2, 0, -1)
and then try1.0 in range(10**10, 0, -1)
– wim
Jan 5 at 23:15
|
show 2 more comments
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.
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 doin
tests onrange
objects. I'm not so sure about its efficiency on Python 2xrange
, 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. Try1.0 in range(10**2, 0, -1)
and then try1.0 in range(10**10, 0, -1)
– wim
Jan 5 at 23:15
|
show 2 more comments
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.
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.
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 doin
tests onrange
objects. I'm not so sure about its efficiency on Python 2xrange
, 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. Try1.0 in range(10**2, 0, -1)
and then try1.0 in range(10**10, 0, -1)
– wim
Jan 5 at 23:15
|
show 2 more comments
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 doin
tests onrange
objects. I'm not so sure about its efficiency on Python 2xrange
, 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. Try1.0 in range(10**2, 0, -1)
and then try1.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
|
show 2 more comments
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"
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
add a comment |
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"
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
add a comment |
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"
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"
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
Python 2.x supports has_key()
.
Python 2.3+ and Python 3.x support in
.
1
Yes, andhas_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
add a comment |
Python 2.x supports has_key()
.
Python 2.3+ and Python 3.x support in
.
1
Yes, andhas_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
add a comment |
Python 2.x supports has_key()
.
Python 2.3+ and Python 3.x support in
.
Python 2.x supports has_key()
.
Python 2.3+ and Python 3.x support in
.
edited Sep 24 '14 at 11:51
ArtOfWarfare
12.9k887138
12.9k887138
answered May 9 '12 at 8:06
SiriusKoderSiriusKoder
3002412
3002412
1
Yes, andhas_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
add a comment |
1
Yes, andhas_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
add a comment |
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
3
No, you inverted the test.t.has_key(ew)
returnsTrue
if the valueew
references is also a key in the dictionary.key not in t
returnsTrue
if the value is not in the dictionary. Moreover, thekey = ew
alias is very, very redundant. The correct spelling isif ew in t
. Which is what the accepted answer from 8 years prior already told you.
– Martijn Pieters♦
Jan 5 at 19:17
add a comment |
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
3
No, you inverted the test.t.has_key(ew)
returnsTrue
if the valueew
references is also a key in the dictionary.key not in t
returnsTrue
if the value is not in the dictionary. Moreover, thekey = ew
alias is very, very redundant. The correct spelling isif ew in t
. Which is what the accepted answer from 8 years prior already told you.
– Martijn Pieters♦
Jan 5 at 19:17
add a comment |
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
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
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)
returnsTrue
if the valueew
references is also a key in the dictionary.key not in t
returnsTrue
if the value is not in the dictionary. Moreover, thekey = ew
alias is very, very redundant. The correct spelling isif ew in t
. Which is what the accepted answer from 8 years prior already told you.
– Martijn Pieters♦
Jan 5 at 19:17
add a comment |
3
No, you inverted the test.t.has_key(ew)
returnsTrue
if the valueew
references is also a key in the dictionary.key not in t
returnsTrue
if the value is not in the dictionary. Moreover, thekey = ew
alias is very, very redundant. The correct spelling isif 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
add a comment |
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?