Python: Access members of a set

Posted by emu on Stack Overflow See other posts from Stack Overflow or by emu
Published on 2012-06-17T14:43:27Z Indexed on 2012/06/17 15:16 UTC
Read the original article Hit count: 151

Filed under:
|
|

Say I have a set myset of custom objects that may be equal although their references are different (a == b and a is not b). Now if I add(a) to the set, Python correctly assumes that a in myset and b in myset even though there is only len(myset) == 1 object in the set.

That is clear. But is it now possible to extract the value of a somehow out from the set, using b only? Suppose that the objects are mutable and I want to change them both, having forgotten the direct reference to a. Put differently, I am looking for the myset[b] operation, which would return exactly the member a of the set.

It seems to me that the type set cannot do this (faster than iterating through all its members). If so, is there at least an effective work-around?

© Stack Overflow or respective owner

Related posts about python

Related posts about python-3.x