Looking up a dictionary _key_ by key?

Paul Rubin no.email at nospam.invalid
Tue Jun 23 21:06:45 EDT 2015


Chris Angelico <rosuav at gmail.com> writes:
>> Would I have to do an O(n) search to find my key?
> Iterate over it - it's an iterable view in Py3 - and compare.

I think the question was whether the O(n) search could be avoided, not
how to do it.  I don't see a way to avoid it.  There is fundamental
brokenness in having unequal objects compare as equal, and the breakage
messes up the dictionary when those objects are used as keys.

Solution is to either fix the object equality test, or wrap them in
something (maybe a tuple containing the objects and the distinguishing
fields that are missing from the original object's equality method) that
treats unequal objects as unequal.



More information about the Python-list mailing list