Re: Way for see if dict has a key



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.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb@xxxxxxxxxxx'.split('@')])"
.



Relevant Pages

  • Re: Help With PyParsing of output from win32pdhutil.ShowAllProcesses()
    ... None to a string). ... Still have an urge to parse with pyparsing? ... pyparsing Dict class. ... keys from the parsed data, so it looks like you are already thinking ...
    (comp.lang.python)
  • Re: Dictionary .keys() and .values() should return a set [with Python 3000 in mind]
    ... keyset of a dictionary be represented as a set instead of a list? ... is as much a product of historical progression as anything: lists and dicts ... so the collection of keys had to be returned as a list. ... of the elements in the dict.keys() list had no bearing on the dict, ...
    (comp.lang.python)
  • Re: Floats as keys in dict
    ... store the nodes I have in play as keys in a dict. ... the dict keys are then floats and I have to round the values ... give the same hash for two numbers that are "close enough" (e.g., ...
    (comp.lang.python)
  • Re: Sets in Python
    ... allow mutable items as keys is a bit arbitrary. ... The user doesn't need to know the mechanism, but the dict does. ... implemented as hash tables. ... Allow the hash of mutable objects to change, ...
    (comp.lang.python)
  • Re: Why are tuples immutable?
    ... If hash equals id, then the first of those cases fails. ... to key the dict. ... I need to know the order of the keys in that list. ... >keys being mutable objects, that they want to mutate those object while ...
    (comp.lang.python)