Looking up a dictionary _key_ by key?

Ian Kelly ian.g.kelly at gmail.com
Wed Jun 24 02:50:46 EDT 2015


On Tue, Jun 23, 2015 at 7:06 PM, Paul Rubin <no.email at nospam.invalid> wrote:
> 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.

I don't think that it's fundamentally broken. A simple example would
be the int 3, vs. the float 3, vs. the Decimal 3. All of them compare
equal to one another, but they are distinct values, and sometimes it
might be useful to be able to determine which one is actually a key in
the dict.



More information about the Python-list mailing list