Re: Way for see if dict has a key



Bruno Desthuilliers wrote:
Fredrik Lundh wrote:
Bruno Desthuilliers wrote:

on my machine, "key in dict" is about twice as fast as the full

try/getitem construct when the key is present in the dict,


Doesn't it depends on the number of keys in the dict ?


why would it depend on the number of keys in the dict ?

</F>

Seems that if "key in dict" do a simple linear search, it depends on the
number of keys in dict (and the position of the searched key etc...).

And if I'm missing the point and you it and you know why, it would be
simple to explain than to answer my question with another question.


maybee we can imagine that key in dict
just perform
try:
dict[key]
return True
except KeyError:
return False

If I understand correctly, it will be time constant no ??
just the needed time to compute the hash for the key

Eric

.



Relevant Pages

  • RE: Way for see if dict has a key
    ... On Behalf Of Georg Brandl ... Bruno Desthuilliers wrote: ... If know only one context: see if the key are into the dict... ...
    (comp.lang.python)
  • Re: setting an attribute
    ... On May 16, 2:24 am, Bruno Desthuilliers <bruno. ... entry for the attribute in the instance ... Since there's no binding, ... instance's dict is not affected. ...
    (comp.lang.python)
  • Re: Way for see if dict has a key
    ... Michele Petrazzo wrote: ... what the preferred way for see if the dict has a key? ... Depends on the context. ...
    (comp.lang.python)