"set".
Sets provide constant-time operations to insert, remove, or check for the presence of a value.
Sets are implemented using a hash table, and therefore, just like keys of a
dictionary, elements of a set must be hashable. A value may be used as an
element of a set if and only if it may be used as a key of a dictionary.
Sets may be constructed using the set() built-in
function, which returns a new set containing the unique elements of its optional argument, which
must be an iterable. Calling set() without an argument constructs an empty set. Sets
have no literal syntax.
The in and not in operations check whether a value is (or is not) in a
set:
for loop, a list
comprehension, and the various built-in functions that operate on iterables. Its length can be
retrieved using the len() built-in function, and the
order of iteration is the order in which elements were first added to the set:
== and !=. A set
s is equal to t if and only if t is a set containing the same
elements; iteration order is not significant. In particular, a set is not equal to the list
of its elements. Sets are not ordered with respect to other sets, and an attempt to compare two sets
using <, <=, >, >=, or to sort a
sequence of sets, will fail.
| operation on two sets returns the union of the two sets: a set containing the
elements found in either one or both of the original sets.
& operation on two sets returns the intersection of the two sets: a set
containing only the elements found in both of the original sets.
- operation on two sets returns the difference of the two sets: a set containing
the elements found in the left-hand side set but not the right-hand side set.
^ operation on two sets returns the symmetric difference of the two sets: a set
containing the elements found in exactly one of the two original sets, but not in both.
|=, &=, -=,
and ^=, modify the left-hand set in place.
Members
- add
- clear
- difference
- difference_update
- discard
- intersection
- intersection_update
- isdisjoint
- issubset
- issuperset
- pop
- remove
- symmetric_difference
- symmetric_difference_update
- union
- update
add
add a value already present in the set; this leaves the set
unchanged.
If you need to add multiple elements to a set, see update or
the |= augmented assignment operation.
Parameters
ParameterDescriptionelement
required
Element to add.
clear
difference
s and t are sets, s.difference(t) is equivalent to
s - t; however, note that the - operation requires both sides to be sets,
while the difference method also accepts sequences and dicts.
It is permissible to call difference without any arguments; this returns a copy of
the set.
For example,
Parameters
ParameterDescriptionothers
required
Collections of hashable elements.
difference_update
s and t are sets, s.difference_update(t) is equivalent
to s -= t; however, note that the -= augmented assignment requires both
sides to be sets, while the difference_update method also accepts sequences and dicts.
It is permissible to call difference_update without any arguments; this leaves the
set unchanged.
For example,
Parameters
ParameterDescriptionothers
required
Collections of hashable elements.
discard
discard a value not present in the set; this leaves the set
unchanged. If you want to fail on an attempt to remove a non-present element, use
remove instead. If you need to remove multiple elements from a
set, see difference_update or the -=
augmented assignment operation.
For example,
Parameters
ParameterDescriptionelement
required
Element to discard. Must be hashable.
intersection
s and t are sets, s.intersection(t) is equivalent to
s & t; however, note that the & operation requires both sides to
be sets, while the intersection method also accepts sequences and dicts.
It is permissible to call intersection without any arguments; this returns a copy of
the set.
For example,
Parameters
ParameterDescriptionothers
required
Collections of hashable elements.
intersection_update
s and t are sets, s.intersection_update(t) is
equivalent to s &= t; however, note that the &= augmented
assignment requires both sides to be sets, while the intersection_update method also
accepts sequences and dicts.
It is permissible to call intersection_update without any arguments; this leaves the
set unchanged.
For example,
Parameters
ParameterDescriptionothers
required
Collections of hashable elements.
isdisjoint
Parameters
ParameterDescriptionother
required
A collection of hashable elements.
issubset
Parameters
ParameterDescriptionother
required
A collection of hashable elements.
issuperset
Parameters
ParameterDescriptionother
required
A collection of hashable elements.
pop
remove
remove fails if the element was not present in the set. If you don’t want to fail on
an attempt to remove a non-present element, use discard instead.
If you need to remove multiple elements from a set, see
difference_update or the -= augmented
assignment operation.
Parameters
ParameterDescriptionelement
required
Element to remove. Must be an element of the set (and hashable).
symmetric_difference
s and t are sets, s.symmetric_difference(t) is
equivalent to s ^ t; however, note that the ^ operation requires both
sides to be sets, while the symmetric_difference method also accepts a sequence or a
dict.
For example,
Parameters
ParameterDescriptionother
required
A collection of hashable elements.
symmetric_difference_update
s and t are sets, s.symmetric_difference_update(t) is
equivalent to `s ^= t ; however, note that the ^=` augmented assignment requires both
sides to be sets, while the symmetric_difference_update method also accepts a sequence
or a dict.
For example,
Parameters
ParameterDescriptionother
required
A collection of hashable elements.
union
s and t are sets, s.union(t) is equivalent to
s | t; however, note that the | operation requires both sides to be sets,
while the union method also accepts sequences and dicts.
It is permissible to call union without any arguments; this returns a copy of the
set.
For example,
Parameters
ParameterDescriptionothers
required
Collections of hashable elements.
update
s and t are sets, s.update(t) is equivalent to
s |= t; however, note that the |= augmented assignment requires both sides
to be sets, while the update method also accepts sequences and dicts.
It is permissible to call update without any arguments; this leaves the set
unchanged.
Parameters
ParameterDescriptionothers
required
Collections of hashable elements.