Retrieving an object from a set

Ian Kelly ian.g.kelly at gmail.com
Sat Jan 26 00:30:50 CET 2013


On Fri, Jan 25, 2013 at 4:14 PM, Arnaud Delobelle <arnodel at gmail.com> wrote:
> Dear Pythoneers,
>
> I've got a seemingly simple problem, but for which I cannot find a
> simple solution.
>
> I have a set of objects (say S) containing an object which is equal to
> a given object (say x). So
>
>     x in S
>
> is true.  So there is an object y in S which is equal to x.  My
> problem is how to retrieve y, without going through the whole set.

You could use a dict.

>>> y = (1, 2, 3)
>>> S = {x: x for x in [y] + range(10000)}
>>> x = (1, 2, 3)
>>> x in S
True
>>> x is y
False
>>> S[x] is y
True



More information about the Python-list mailing list