Instances of Set and ImmutableSet both provide the following operations:
Operation | Equivalent | Result |
---|---|---|
len(s) |
cardinality of set s | |
x in s |
test x for membership in s | |
x not in s |
test x for non-membership in s | |
s.issubset(t) |
s <= t |
test whether every element in s is in t |
s.issuperset(t) |
s >= t |
test whether every element in t is in s |
s.union(t) |
s | t | new set with elements from both s and t |
s.intersection(t) |
s & t | new set with elements common to s and t |
s.difference(t) |
s - t | new set with elements in s but not in t |
s.symmetric_difference(t) |
s ^ t | new set with elements in either s or t but not both |
s.copy() |
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
Set('abc').intersection('cbs')
.
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
False
: a<b
, a==b
, or
a>b
.
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:
Operation | Result |
---|---|
hash(s) |
returns a hash value for s |
The following table lists operations available in Set but not found in ImmutableSet:
Operation | Equivalent | Result |
---|---|---|
s.union_update(t) |
s |= t | return set s with elements added from t |
s.intersection_update(t) |
s &= t | return set s keeping only elements also found in t |
s.difference_update(t) |
s -= t | return set s after removing elements found in t |
s.symmetric_difference_update(t) |
s ^= t | return set s with elements from s or t but not both |
s.add(x) |
add element x to set s | |
s.remove(x) |
remove x from set s; raises KeyError if not present | |
s.discard(x) |
removes x from set s if present | |
s.pop() |
remove and return an arbitrary element from s; raises KeyError if empty | |
s.clear() |
remove all elements from set s |
Note, the non-operator versions of union_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.
See About this document... for information on suggesting changes.