153

I'm trying to make a set of sets in Python. I can't figure out how to do it.

Starting with the empty set xx:

xx = set([])
# Now we have some other set, for example
elements = set([2,3,4])
xx.add(elements)

but I get

TypeError: unhashable type: 'list'

or

TypeError: unhashable type: 'set'

Is it possible to have a set of sets in Python?

I am dealing with a large collection of sets and I want to be able to not have to deal duplicate sets (a set B of sets A1, A2, ...., An would "cancel" two sets if Ai = Aj)

4 Answers 4

146

Python's complaining because the inner set objects are mutable and thus not hashable. The solution is to use frozenset for the inner sets, to indicate that you have no intention of modifying them.

xx = set([])
# Nested sets must be frozen
elements = frozenset([2,3,4])
xx.add(elements)
72

People already mentioned that you can do this with a frozenset(), so I will just add a code how to achieve this:

For example you want to create a set of sets from the following list of lists:

t = [[], [1, 2], [5], [1, 2, 5], [1, 2, 3, 4], [1, 2, 3, 6]]

you can create your set in the following way:

t1 = set(frozenset(i) for i in t)
1
  • 17
    or you can use map! set(map(frozenset, t))
    – Matt Dodge
    Jan 6, 2017 at 19:55
20

Use frozenset inside.

5
  • 14
    Perhaps you could give a few pointers about mutable/immutable objects in Python since he's new? May 9, 2011 at 0:18
  • 2
    @Seth: I could, but mutability is not a factor. May 9, 2011 at 0:19
  • Thanks very much! Just reading re: mutability now. Seems like a set of lists may also work but frozenset seems to get it done. Thanks again!
    – Matt
    May 9, 2011 at 0:23
  • @Ignacio I thought that members in sets and keys in dicts had to be hash-able and therefore immutable. May 9, 2011 at 1:19
  • 8
    Hashability and mutability are not necessarily mutually-exclusive. It just so happens that most of the basic Python types share a pattern. May 9, 2011 at 1:54
3

So I had the exact same problem. I wanted to make a data structure that works as a set of sets. The problem is that the sets must contain immutable objects. So, what you can do is simply make it as a set of tuples. That worked fine for me!

A = set()
A.add( (2,3,4) )##adds the element
A.add( (2,3,4) )##does not add the same element
A.add( (2,3,5) )##adds the element, because it is different!
1
  • 31
    In tuples, the element order matters. Thus A.add( (4,3,2)); A.add((2,4,3)); A.add((2,3,4)) will add three distinct elements, while the original question is about "set of sets", which implies that (2,3,4), (4,3,2), (2,4,3) are the same. Feb 28, 2017 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.