Instances of Set and ImmutableSet both provide the following operations:
Operation | 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) |
test whether every element in s is in t;
s <= t is equivalent |
s.issuperset(t) |
test whether every element in t is in s;
s >= t is equivalent |
s | t |
new set with elements from both s and t |
s.union(t) |
new set with elements from both s and t |
s & t |
new set with elements common to s and t |
s.intersection(t) |
new set with elements common to s and t |
s - t |
new set with elements in s but not in t |
s.difference(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 |
s.symmetric_difference(t) |
new set with elements in either s or t but not both |
s.copy() |
new set with a shallow copy of s |
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 none of the following are true:
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 | Result |
---|---|
s |= t |
return set s with elements added from t |
s.union_update(t) |
return set s with elements added from t |
s &= t |
return set s keeping only elements also found in t |
s.intersection_update(t) |
return set s keeping only elements also found in t |
s -= t |
return set s after removing elements found in t |
s.difference_update(t) |
return set s after removing elements found in t |
s ^= t |
return set s with elements from s or t but not both |
s.symmetric_difference_update(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 |
s.discard(x) |
removes x from set s if present |
s.pop() |
remove and return an arbitrary element from s |
s.update(t) |
add elements from t to set s |
s.clear() |
remove all elements from set s |
See About this document... for information on suggesting changes.