Instances of Set and ImmutableSet both provide the following operations:
||cardinality of set s|
||test x for membership in s|
||test x for non-membership in s|
||test whether every element in s is in t|
||test whether every element in t is in s|
||s | t||new set with elements from both s and t|
||s & t||new set with elements common to s and t|
||s - t||new set with elements in s but not in t|
||s ^ t||new set with elements in either s or t but not both|
||new set with a shallow copy of s|
Note, the non-operator versions of union(),
intersection(), difference(), and
symmetric_difference() will accept any iterable as an argument.
In contrast, their operator based counterparts require their arguments to
be sets. This precludes error-prone constructions like
Set('abc') & 'cbs' in favor of the more readable
Changed in version 2.3.1:
Formerly all arguments were required to be sets.
In addition, both Set and ImmutableSet support set to set comparisons. Two sets are equal if and only if every element of each set is contained in the other (each is a subset of the other). A set is less than another set if and only if the first set is a proper subset of the second set (is a subset, but is not equal). A set is greater than another set if and only if the first set is a proper superset of the second set (is a superset, but is not equal).
The subset and equality comparisons do not generalize to a complete
ordering function. For example, any two disjoint sets are not equal and
are not subsets of each other, so all of the following return
Accordingly, sets do not implement the __cmp__ method.
Since sets only define partial ordering (subset relationships), the output of the list.sort() method is undefined for lists of sets.
The following table lists operations available in ImmutableSet but not found in Set:
||returns a hash value for s|
The following table lists operations available in Set but not found in ImmutableSet:
||s |= t||return set s with elements added from t|
||s &= t||return set s keeping only elements also found in t|
||s -= t||return set s after removing elements found in t|
||s ^= t||return set s with elements from s or t but not both|
||add element x to set s|
||remove x from set s; raises KeyError if not present|
||removes x from set s if present|
||remove and return an arbitrary element from s; raises KeyError if empty|
||remove all elements from set s|
Note, the non-operator versions of update(), intersection_update(), difference_update(), and symmetric_difference_update() will accept any iterable as an argument. Changed in version 2.3.1: Formerly all arguments were required to be sets.
Also note, the module also includes a union_update() method which is an alias for update(). The method is included for backwards compatibility. Programmers should prefer the update() method because it is supported by the builtin set() and frozenset() types.
See About this document... for information on suggesting changes.