list of unique non-subset sets

Kent Johnson kent37 at
Thu Mar 17 21:53:55 CET 2005

Raymond Hettinger wrote:
> [les_ander at]
>>I have many set objects some of which can contain same group of object
>>while others can be subset of the other. Given a list of sets,
>>I need to get a list of unique sets such that non of the set is an
>>subset of another or contain exactly the same members.
>>Tried to do the following:
>>----------------------- > cleaned-up list should contain s1, s3, s5
> This should do the trick:
> result = []
> for s1 in L:
>     for s2 in result:
>         if s1 <= s2:
>             break
>     else:
>         result.append(s1)
> print result

If I understand the original post correctly, you also need to check for an existing set being a 
subset of the set you are adding. A better test case is
s6=set(['g', 'h'])
s7=set(['h', 'i'])
s8=set(['g', 'h', 'i'])

# ----------------------- > cleaned-up list should contain s1, s3, s5, s8

which both Raymond and STeVe's proposals fail.


More information about the Python-list mailing list