Mathematically speaking, a set is an unordered collection of zero or more distinct elements. Python has two set types that represent this mathematical abstraction. Use these types when you care only about whether something is a member of the set or not, and you don't need them to be in any specific order.
The elements of a Python set must be immutable. In particular, you can't have list or dictionary elements in a set.
Most operations on sets work with both set and
frozenset types.
Values of type set are mutable: you can add
or delete members.
A frozenset value is immutable: you can't
change the membership, but you can use a frozenset value in contexts where set values are not allowed. For example, you can use a frozenset as a key in a dictionary, but you can't
use a set value as a dictionary key.
To create a set or frozenset, see
Section 13.33, “set(): Create an algebraic set” and Section 13.14, “frozenset(): Create a frozen set”.
A number of functions that work on sequences also work on sets. In each case, the set is converted to a list before being passed to the function.
Section 13.2, “all(): Are all the elements of a
sequence true?”. Predicate to test whether
all members of a set are True.
Section 13.3, “any(): Are any of the members of a
sequence true?”. Predicate to test whether
any member of a set is true.
Section 13.12, “filter(): Extract qualifying elements
from a sequence”. Returns a list
of the elements that pass through a filtering function.
Section 13.17, “iter(): Produce an iterator over a
sequence”. Returns an iterator
that will visit every element of the set.
Section 13.18, “len(): Number of elements”. Returns the length
(cardinality) of the set.
Section 13.19, “list(): Convert to a list”. Returns the elements
of the set as a list.
Section 13.21, “map(): Apply a function to each
element of a sequence”. Returns a list
containing the result of the application of a function
to each element of a set.
Section 13.22, “max(): Largest element of a
sequence”. Returns the largest
element of a set.
Section 13.23, “min(): Smallest element of a
sequence”. Returns the smallest
element of a set.
Section 13.30, “reduce(): Sequence reduction”. Returns the result
of the application of a given function pairwise to all
the elements of a set.
Section 13.34, “sorted(): Sort a sequence”. Returns a list
containing the sorted elements of the set.