# Dual look-up on keys?

castironpi at gmail.com castironpi at gmail.com
Thu Mar 6 03:03:28 CET 2008

```On Mar 5, 5:31 pm, Grant Edwards <gra... at visi.com> wrote:
> On 2008-03-05, castiro... at gmail.com <castiro... at gmail.com> wrote:
> > Anyway, if (a,b) is a key in dictionary d, can it guarantee
> > that (b,a) is also in it, and maps to the same object?

Er... -specialized- dictionary d.

> To solve that problem, Python provides the immutable
> "frozenset" type:
>
>   >>> s1 = frozenset((1,2))
>   >>> s2 = frozenset((2,1))
>   >>> s1 == s2
>   True
>   >>> d = {s1: "hi there"}
>   >>> s1 in d
>   True
>   >>> s2 in d
>   True

Ah.  Perfect.  With this, I can just call frozenset on keys in
__setitem__ and __getitem__... (though at that, it may be easier
verbatim*.)

*loosely introduced terminology, roughly equivalent to 'spell it out
by hand each time'**.
**__delitem__ too.

Only thing is, that's an extra check if 'key' is iterable.

(Yes.  I realize this is a little scattered... it's my belief it's
intelligible by party X... if not stop now speak up.)

Goal:
assert d[1,2] is d[2,1]
assert d[1] is d[1]
assert d[a] is d[a]
assert d[a,b] is d[b,a]

Only problem is, if a is iterable, then its contents get hashed.  I
don't see the general way around this.

a= SomeList( [ 1,2,3 ] )
b= SomeList( [ 1,2,3 ] )
assert a is not b
d[a]= True
assert b not in d
#there's the hangup

It is my loss that M. Roggisch plonks me-- I will be deprived of his
perspective and contributions.

```