Retrieving an object from a set

Dave Angel d at
Sat Jan 26 00:56:14 CET 2013

On 01/25/2013 06:14 PM, Arnaud Delobelle 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.
> Here is a simple illustration with tuples (my actual scenario is not
> with tuples but with a custom class):
>>>> y = (1, 2, 3) # This is the 'hidden object'
>>>> S = set([y] + range(10000))
>>>> x = (1, 2, 3)
>>>> x in S
> True
>>>> x is y
> False
> I haven't found y.

Baloney.  You've got the item y which is equal to x, not identical to x. 
  So just what did you expect?  "is" is the wrong comparator.

What exactly is your problem?


More information about the Python-list mailing list