Python remove set from set

You already answered the question. It refers to sets of sets (actually sets containing frozensets).

The paragraph you are referring to begins with:

Note, the elem argument to the __contains__(), remove(), and discard() methods may be a set.

which means that b in a.remove(b) can be a set, and then continues with:

To support searching for an equivalent frozenset, the elem set is temporarily mutated during the search and then restored. During the search, the elem set should not be read or mutated since it does not have a meaningful value.

which means that if b is a set, a.remove(b) will scan a for a frozenset equivalent to b and remove it (or throw a KeyError if it doesn't exist).


You can't have sets of sets in Python as a set is mutable. Instead, you can have sets of frozensets. On the other hand, you can call __contains__(), remove(), and discard() with a set. See this example:

a = set([frozenset([2])])
set([2]) in a       # you get True
a.remove(set([2]))  # a is now empty

So the answer to your question is that the documentation is referring to sets of frozensets.


set1-set2

set1 = {0,1,2,3}
set2 = {2,3,4,5}

set1 - set2  # {0, 1}
set2 - set1  # {4, 5}

However, note that for whatever reason you can't "+" sets in python...

Tags:

Python

Set