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.
There are two ways to create a mutable set.
In all
Python versions of the 2.x series, the set( function
operates on a sequence S) and returns a mutable set
containing the unique elements of S. The argument is optional;
if omitted, you get a new, empty set.
S
>>> s1 = set([1, 1, 1, 9, 1, 8, 9, 8, 3])
set([8, 1, 3, 9])
>>> s1 = set([1, 1, 1, 9, 1, 8, 9, 8, 3])
>>> s2 = set()
>>> s1
set([8, 1, 3, 9])
>>> s2
set([])
>>> print len(s1), len(s2)
4 0
>>> s3 = set("notlob bolton")
>>> s3
s3
set([' ', 'b', 'l', 'o', 'n', 't'])
Starting in Python 2.7, you can create a set by
simply enclosing one or more elements within braces
{...} separated by commas.
s1 = {1, 1, 1, 9, 1, 8, 9, 8, 3}
>>> s1
set([8, 9, 3, 1])
Note the wording “one or more:” an
empty pair of braces “{}” is an empty dictionary, not an empty set.
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 20.38, “set(): Create an algebraic set” and Section 20.17, “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 20.2, “all(): Are all the elements of an
iterable true?”. Predicate to test whether
all members of a set are True.
Section 20.3, “any(): Are any of the members of an
iterable true?”. Predicate to test whether
any member of a set is true.
Section 20.14, “filter(): Extract qualifying elements
from an iterable”. Returns a list
of the elements that pass through a filtering function.
Section 20.21, “iter(): Produce an iterator over a
sequence”. Returns an iterator
that will visit every element of the set.
Section 20.22, “len(): Number of elements”. Returns the length
(cardinality) of the set.
Section 20.23, “list(): Convert to a list”. Returns the elements
of the set as a list.
Section 20.25, “map(): Apply a function to each
element of an iterable”. Returns a list
containing the result of the application of a function
to each element of a set.
Section 20.26, “max(): Largest element of an
iterable”. Returns the largest
element of a set.
Section 20.27, “min(): Smallest element of an
iterable”. Returns the smallest
element of a set.
Section 20.35, “reduce(): Sequence reduction”. Returns the result
of the application of a given function pairwise to all
the elements of a set.
Section 20.39, “sorted(): Sort a sequence”. Returns a list
containing the sorted elements of the set.
Another new feature in Python 2.7 is the set comprehension. This is similar to the feature described in Section 11.2, “List comprehensions”. Here is the general form:
{ e
for v1 in s1
for v2 in s2
...
if c }
As with a list comprehension, you use one or more for clauses to iterate over sets of values, and
the expression is evaluated for every combination of the values in the
sequences e. If there is no “siif” clause, or if the “if”
condition evaluates as True, the value is
added to the sequence from which a set is then constructed.
Here is an example. Function takeUppers() takes
one string argument and returns a set of the unique letters in
that string, uppercased. The for clause iterates
over the characters in the argument s; the if clause discards characters that aren't letters; and
the .upper() method converts lowercase letters to
uppercase.
>>> def takeUpper(s):
... return { c.upper()
... for c in s
... if c.isalpha() }
...
>>> takeUpper("A a|ccCc^#zZ")
set(['A', 'C', 'Z'])