Even though their items must all be immutable/hashable types, sets themselves are a mutable/nonhashable type. You can either add or remove items from a set using methods such as set.add
, set.pop
, or set.remove
. Thus, you cannot put a set inside another set because the item might be changed at any time.
Instead, you can use a frozenset
, which is an immutable/hashable set:
>>> {frozenset({2}), 3,4}
set([frozenset([2]), 3, 4])
>>>
Keep in mind however that this only works because frozensets cannot be changed after they are created (there is no way to add or remove items).