Instances ofSet andImmutableSet both providethe following operations:
| Operation | Equivalent | Result |
|---|---|---|
len(s) | cardinality of sets | |
x ins | testx for membership ins | |
x not ins | testx for non-membership ins | |
s.issubset(t) | s <=t | test whether every element ins is int |
s.issuperset(t) | s >=t | test whether every element int is ins |
s.union(t) | s |t | new set with elements from boths andt |
s.intersection(t) | s &t | new set with elements common tos andt |
s.difference(t) | s -t | new set with elements ins but not int |
s.symmetric_difference(t) | s ^t | new set with elements in eithers ort but not both |
s.copy() | new set with a shallow copy ofs |
Note, the non-operator versions ofunion(),intersection(),difference(), andsymmetric_difference() will accept any iterable as an argument.In contrast, their operator based counterparts require their arguments tobe sets. This precludes error-prone constructions likeSet('abc') & 'cbs' in favor of the more readableSet('abc').intersection('cbs').Changed in version 2.3.1:Formerly all arguments were required to be sets.
In addition, bothSet andImmutableSetsupport set to set comparisons. Two sets are equal if and only ifevery element of each set is contained in the other (each is a subsetof the other).A set is less than another set if and only if the first set is a propersubset 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 propersuperset of the second set (is a superset, but is not equal).
The subset and equality comparisons do not generalize to a completeordering function. For example, any two disjoint sets are not equal andare not subsets of each other, soall of the following returnFalse:a<b,a==b, ora>b.Accordingly, sets do not implement the__cmp__ method.
Since sets only define partial ordering (subset relationships), the outputof thelist.sort() method is undefined for lists of sets.
The following table lists operations available inImmutableSetbut not found inSet:
| Operation | Result |
|---|---|
hash(s) | returns a hash value fors |
The following table lists operations available inSetbut not found inImmutableSet:
| Operation | Equivalent | Result |
|---|---|---|
s.update(t) | s |=t | return sets with elements added fromt |
s.intersection_update(t) | s &=t | return sets keeping only elements also found int |
s.difference_update(t) | s -=t | return sets after removing elements found int |
s.symmetric_difference_update(t) | s ^=t | return sets with elements froms ort but not both |
s.add(x) | add elementx to sets | |
s.remove(x) | removex from sets; raisesKeyError if not present | |
s.discard(x) | removesx from sets if present | |
s.pop() | remove and return an arbitrary element froms; raisesKeyError if empty | |
s.clear() | remove all elements from sets |
Note, the non-operator versions ofupdate(),intersection_update(),difference_update(), andsymmetric_difference_update() will accept any iterable asan argument.Changed in version 2.3.1:Formerly all arguments were required to be sets.
Also note, the module also includes aunion_update() methodwhich is an alias forupdate(). The method is included forbackwards compatibility. Programmers should prefer theupdate() method because it is supported by the builtinset() andfrozenset() types.