1

I want to find the unique elements in A =[set([1,2]),set([1,2]), set([1])] in Python. I tried set(A); it didn't work. Is there any easy way to do it?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
pmjn6
  • 307
  • 1
  • 4
  • 14

1 Answers1

4

Convert your sets to frozenset() objects:

set(frozenset(s) for s in A)

A frozenset() is an immutable set object, and more importantly, hashable. Thus it can be stored in a set().

Demo:

>>> A = [set([1,2]),set([1,2]), set([1])]
>>> set(frozenset(s) for s in A)
set([frozenset([1, 2]), frozenset([1])])
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343