blob: 74d09c791ff3168b95925040a24dde9d945b6351 [file] [log] [blame]
Raymond Hettinger584cb192002-08-23 15:18:38 +00001\section{\module{sets} ---
2 Unordered collections of unique elements}
3
4\declaremodule{standard}{sets}
5\modulesynopsis{Implementation of sets of unique elements.}
6\moduleauthor{Greg V. Wilson}{gvwilson@nevex.com}
7\moduleauthor{Alex Martelli}{aleax@aleax.it}
8\moduleauthor{Guido van Rossum}{guido@python.org}
9\sectionauthor{Raymond D. Hettinger}{python@rcn.com}
10
11\versionadded{2.3}
12
13The \module{sets} module provides classes for constructing and manipulating
14unordered collections of unique elements. Common uses include membership
15testing, removing duplicates from a sequence, and computing standard math
16operations on sets such as intersection, union, difference, and symmetric
17difference.
18
Fred Draked10c6c92002-08-23 17:22:36 +000019Like other collections, sets support \code{\var{x} in \var{set}},
20\code{len(\var{set})}, and \code{for \var{x} in \var{set}}. Being an
21unordered collection, sets do not record element position or order of
22insertion. Accordingly, sets do not support indexing, slicing, or
23other sequence-like behavior.
Raymond Hettinger584cb192002-08-23 15:18:38 +000024
25Most set applications use the \class{Set} class which provides every set
26method except for \method{__hash__()}. For advanced applications requiring
27a hash method, the \class{ImmutableSet} class adds a \method{__hash__()}
28method but omits methods which alter the contents of the set. Both
29\class{Set} and \class{ImmutableSet} derive from \class{BaseSet}, an
30abstract class useful for determining whether something is a set:
Fred Draked10c6c92002-08-23 17:22:36 +000031\code{isinstance(\var{obj}, BaseSet)}.
Raymond Hettinger584cb192002-08-23 15:18:38 +000032
Fred Draked10c6c92002-08-23 17:22:36 +000033The set classes are implemented using dictionaries. As a result, sets
34cannot contain mutable elements such as lists or dictionaries.
35However, they can contain immutable collections such as tuples or
36instances of \class(ImmutableSet). For convenience in implementing
37sets of sets, inner sets are automatically converted to immutable
38form, for example, \code{Set([Set(['dog'])])} is transformed to
Raymond Hettinger584cb192002-08-23 15:18:38 +000039\code{Set([ImmutableSet(['dog'])])}.
40
41\begin{classdesc}{Set}{\optional{iterable}}
42Constructs a new empty \class{Set} object. If the optional \var{iterable}
43parameter is supplied, updates the set with elements obtained from iteration.
44All of the elements in \var{iterable} should be immutable or be transformable
Fred Draked10c6c92002-08-23 17:22:36 +000045to an immutable using the protocol described in
46section~\ref{immutable-transforms}.
Raymond Hettinger584cb192002-08-23 15:18:38 +000047\end{classdesc}
48
49\begin{classdesc}{ImmutableSet}{\optional{iterable}}
50Constructs a new empty \class{ImmutableSet} object. If the optional
51\var{iterable} parameter is supplied, updates the set with elements obtained
52from iteration. All of the elements in \var{iterable} should be immutable or
Fred Draked10c6c92002-08-23 17:22:36 +000053be transformable to an immutable using the protocol described in
54section~\ref{immutable-transforms}.
Raymond Hettinger584cb192002-08-23 15:18:38 +000055
56Because \class{ImmutableSet} objects provide a \method{__hash__()} method,
57they can be used as set elements or as dictionary keys. \class{ImmutableSet}
58objects do not have methods for adding or removing elements, so all of the
59elements must be known when the constructor is called.
60\end{classdesc}
61
62
Fred Draked10c6c92002-08-23 17:22:36 +000063\subsection{Set Objects}
Raymond Hettinger584cb192002-08-23 15:18:38 +000064
65Instances of \class{Set} and \class{ImmutableSet} both provide
66the following operations:
67
Fred Draked10c6c92002-08-23 17:22:36 +000068\begin{tableii}{c|l}{code}{Operation}{Result}
69 \lineii{len(\var{s})}{cardinality of set \var{s}}
Raymond Hettinger584cb192002-08-23 15:18:38 +000070
71 \hline
Fred Draked10c6c92002-08-23 17:22:36 +000072 \lineii{\var{x} in \var{s}}
73 {test \var{x} for membership in \var{s}}
74 \lineii{\var{x} not in \var{s}}
75 {test \var{x} for non-membership in \var{s}}
76 \lineii{\var{s}.issubset(\var{t})}
77 {test whether every element in \var{s} is in \var{t}}
78 \lineii{\var{s}.issuperset(\var{t})}
79 {test whether every element in \var{t} is in \var{s}}
Raymond Hettinger584cb192002-08-23 15:18:38 +000080
81 \hline
Fred Draked10c6c92002-08-23 17:22:36 +000082 \lineii{\var{s} | \var{t}}
83 {new set with elements from both \var{s} and \var{t}}
84 \lineii{\var{s}.union(\var{t})}
85 {new set with elements from both \var{s} and \var{t}}
86 \lineii{\var{s} \&\ \var{t}}
87 {new set with elements common to \var{s} and \var{t}}
88 \lineii{\var{s}.intersection(\var{t})}
89 {new set with elements common to \var{s} and \var{t}}
90 \lineii{\var{s} - \var{t}}
91 {new set with elements in \var{s} but not in \var{t}}
92 \lineii{\var{s}.difference(\var{t})}
93 {new set with elements in \var{s} but not in \var{t}}
94 \lineii{\var{s} \textasciicircum\ \var{t}}
95 {new set with elements in either \var{s} or \var{t} but not both}
96 \lineii{\var{s}.symmetric_difference(\var{t})}
97 {new set with elements in either \var{s} or \var{t} but not both}
98 \lineii{\var{s}.copy()}
99 {new set with a shallow copy of \var{s}}
100\end{tableii}
Raymond Hettinger584cb192002-08-23 15:18:38 +0000101
102In addition to the above operations, both \class{Set} and \class{ImmutableSet}
103support set to set comparison operators based on the contents of their
104internal dictionaries. Two sets are equal if and only if every element of
105each set is contained in the other.
106
107The following table lists operations available in \class{ImmutableSet}
108but not found in \class{Set}:
109
Fred Draked10c6c92002-08-23 17:22:36 +0000110\begin{tableii}{c|l|c}{code}{Operation}{Result}
111 \lineii{hash(\var{s})}{returns a hash value for \var{s}}
112\end{tableii}
Raymond Hettinger584cb192002-08-23 15:18:38 +0000113
114The following table lists operations available in \class{Set}
115but not found in \class{ImmutableSet}:
116
Fred Draked10c6c92002-08-23 17:22:36 +0000117\begin{tableii}{c|l}{code}{Operation}{Result}
118 \lineii{\var{s} |= \var{t}}
119 {return set \var{s} with elements added from \var{t}}
120 \lineii{\var{s}.union_update(\var{t})}
121 {return set \var{s} with elements added from \var{t}}
122 \lineii{\var{s} \&= \var{t}}
123 {return set \var{s} keeping only elements also found in \var{t}}
124 \lineii{\var{s}.intersection_update(\var{t})}
125 {return set \var{s} keeping only elements also found in \var{t}}
126 \lineii{\var{s} -= \var{t}}
127 {return set \var{s} after removing elements found in \var{t}}
128 \lineii{\var{s}.difference_update(\var{t})}
129 {return set \var{s} after removing elements found in \var{t}}
130 \lineii{\var{s} \textasciicircum= \var{t}}
131 {return set \var{s} with elements from \var{s} or \var{t}
132 but not both}
133 \lineii{\var{s}.symmetric_difference_update(\var{t})}
134 {return set \var{s} with elements from \var{s} or \var{t}
135 but not both}
Raymond Hettinger584cb192002-08-23 15:18:38 +0000136
137 \hline
Fred Draked10c6c92002-08-23 17:22:36 +0000138 \lineii{\var{s}.add(\var{x})}
139 {Add element \var{x} to set \var{s}}
140 \lineii{\var{s}.remove(\var{x})}
141 {Remove element \var{x} from set \var{s}}
142 \lineii{\var{s}.discard(\var{x})}
143 {Removes element \var{x} from set \var{s} like \var{s}.remove(\var{x})
144 but does not raise a KeyError if \var{x} is not in \var{s}}
145 \lineii{\var{s}.pop()}
Tim Peters54fd3e62002-08-23 17:45:43 +0000146 {Remove and return an element from \var{s}; no guarantee is
147 made about which element is removed}
Fred Draked10c6c92002-08-23 17:22:36 +0000148 \lineii{\var{s}.update(\var{t})}
149 {Add elements from \var{t} to set \var{s}}
150 \lineii{\var{s}.clear()}
151 {Remove all elements from set \var{s}}
152\end{tableii}
Raymond Hettinger584cb192002-08-23 15:18:38 +0000153
154
155\subsection{Example}
156
157\begin{verbatim}
158>>> from sets import Set
159>>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
160>>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])
161>>> management = Set(['Jane', 'Jack', 'Susan', 'Zack'])
162>>> employees = engineers | programmers | management # union
163>>> engineering_management = engineers & programmers # intersection
164>>> fulltime_management = management - engineers - programmers # difference
165>>> engineers.add('Marvin') # add element
166>>> print engineers
167Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])
168>>> employees.issuperset(engineers) # superset test
169False
170>>> employees.update(engineers) # update from another set
171>>> employees.issuperset(engineers)
172True
173>>> for group in [engineers, programmers, management, employees]:
174 group.discard('Susan') # unconditionally remove element
175 print group
176
177Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])
178Set(['Janice', 'Jack', 'Sam'])
179Set(['Jane', 'Zack', 'Jack'])
180Set(['Jack', 'Sam', 'Jane', 'Marvin', 'Janice', 'John', 'Zack'])
181\end{verbatim}
182
183
184\subsection{Protocol for automatic conversion to immutable
185 \label{immutable-transforms}}
186
187Sets can only contain immutable elements. For convenience, mutable
188\class{Set} objects are automatically copied to an \class{ImmutableSet}
189before being added as a set element.
190
Fred Draked10c6c92002-08-23 17:22:36 +0000191The mechanism is to always add a hashable element, or if it is not
192hashable, the element is checked to see if it has an
193\method{_as_immutable()} method which returns an immutable equivalent.
Raymond Hettinger584cb192002-08-23 15:18:38 +0000194
195Since \class{Set} objects have a \method{_as_immutable()} method
196returning an instance of \class{ImmutableSet}, it is possible to
197construct sets of sets.
198
199A similar mechanism is needed by the \method{__contains__()} and
200\method{remove()} methods which need to hash an element to check
201for membership in a set. Those methods check an element for hashability
Fred Draked10c6c92002-08-23 17:22:36 +0000202and, if not, check for a \method{_as_Temporarily_Immutable()} method
Raymond Hettinger584cb192002-08-23 15:18:38 +0000203which returns the element wrapped by a class that provides temporary
204methods for \method{__hash__()}, \method{__eq__()}, and \method{__ne__()}.
205
206The alternate mechanism spares the need to build a separate copy of
207the original mutable object.
208
Fred Draked10c6c92002-08-23 17:22:36 +0000209\class{Set} objects implement the \method{_as_Temporarily_Immutable()}
210method which returns the \class{Set} object wrapped by a new class
Raymond Hettinger584cb192002-08-23 15:18:38 +0000211\class{_TemporarilyImmutableSet}.
212
213The two mechanisms for adding hashability are normally invisible to the
214user; however, a conflict can arise in a multi-threaded environment
215where one thread is updating a Set while another has temporarily wrapped it
216in \class{_TemporarilyImmutableSet}. In other words, sets of mutable sets
217are not thread-safe.