Efficiency difference between dict.has_key and key in dict in Python [duplicate]
2024/10/11 22:25:07
Possible Duplicate: 'has_key()' or 'in'?
In Python, there're two ways of deciding whether a key is in a dict:
if dict.has_key(key) and if key in dict
Someone tells me that the second one is slower than the first one since the in keyword makes the expression an iteration over the dict, so it will be slower than the has_key alternative, which apparently uses hash to make the decision.
As I highly doubt the difference, since I think Python is smart enough to translate an in keyword before a dict to some hash way, I can't find any formal claim about this.
So is there really any efficiency difference between the two?
Thanks.
Answer
Both of these operations do the same thing: examine the hash table implemented in the dict for the key. Neither will iterate the entire dictionary. Keep in mind that for x in dict is different than if x in dict. They both use the in keyword, but are different operations.
The in keyword becomes a call on dict.__contains__, which dict can implement however it likes.
If there is a difference in the timings of these operations, it will be very small, and will have to do with the function call overhead of has_key.
BTW, the general preference is for key in dict as a clearer expression of the intent than dict.has_key(key). Note that speed has nothing to do with the preference. Readability is more important than speed unless you know you are in the critical path.
Its a bit weird, I have activated the virtual environment python still points to the global installation.$ which python
/usr/bin/python$ source ~/virtualenv/bin/activate
(virtualenv)$ which python
/usr…
Im writing a text classification system in Python. This is what Im doing to canonicalize each token:lem, stem = WordNetLemmatizer(), PorterStemmer()
for doc in corpus:for word in doc:lemma = stem.stem(…
I have a module with a function (call it a()) that calls another function defined in the same module (call it __b()). __b() is a function which speaks to a website via urllib2 and gets some data back.…
I am currently looking for method in which i can interleave 2 numpy.ndarray. such that>>> a = np.random.rand(5,5)
>>> print a
[[ 0.83367208 0.29507876 0.41849799 0.58342521 0.818…
I am rather new to WTForms, Flask-WTF. I cant figure out how to simply add the HTML5 attribute "autofocus" to one of the form field, from the form definition. I would like to do that in the P…
I have an image and I want to transpose it by 30 degrees. Is it possible to do by using something like the following?spinPicture003 = Picture003.transpose(Image.Rotate_30)
Can anybody suggest how to calculate angle between three points (lat long coordinates)A : (12.92473, 77.6183)
B : (12.92512, 77.61923)
C : (12.92541, 77.61985)
Im using Z3 to solve the Eight Queens puzzle. I know that each queen can be represented by a single integer in this problem. But, when I represent a queen by two integers as following:from z3 import *X…