blob: 5e0edf613c0cc457c442ba85cdddbc1a421041a1 [file] [log] [blame]
Raymond Hettinger96ef8112003-02-01 00:10:11 +00001\section{\module{itertools} ---
2 Functions creating iterators for efficient looping}
3
4\declaremodule{standard}{itertools}
5\modulesynopsis{Functions creating iterators for efficient looping.}
6\moduleauthor{Raymond Hettinger}{python@rcn.com}
7\sectionauthor{Raymond Hettinger}{python@rcn.com}
8\versionadded{2.3}
9
10
11This module implements a number of iterator building blocks inspired
12by constructs from the Haskell and SML programming languages. Each
13has been recast in a form suitable for Python.
14
Raymond Hettinger60eca932003-02-09 06:40:58 +000015The module standardizes a core set of fast, memory efficient tools
16that are useful by themselves or in combination. Standardization helps
17avoid the readability and reliability problems which arise when many
18different individuals create their own slightly varying implementations,
19each with their own quirks and naming conventions.
Raymond Hettinger96ef8112003-02-01 00:10:11 +000020
Raymond Hettinger60eca932003-02-09 06:40:58 +000021The tools are designed to combine readily with each another. This makes
22it easy to construct more specialized tools succinctly and efficiently
23in pure Python.
Raymond Hettinger96ef8112003-02-01 00:10:11 +000024
Raymond Hettinger60eca932003-02-09 06:40:58 +000025For instance, SML provides a tabulation tool: \code{tabulate(\var{f})}
26which produces a sequence \code{f(0), f(1), ...}. This toolbox
27provides \function{imap()} and \function{count()} which can be combined
28to form \code{imap(\var{f}, count())} and produce an equivalent result.
Raymond Hettinger96ef8112003-02-01 00:10:11 +000029
Raymond Hettinger60eca932003-02-09 06:40:58 +000030Whether cast in pure python form or C code, tools that use iterators
31are more memory efficient (and faster) than their list based counterparts.
32Adopting the principles of just-in-time manufacturing, they create
33data when and where needed instead of consuming memory with the
34computer equivalent of ``inventory''.
Raymond Hettinger96ef8112003-02-01 00:10:11 +000035
Raymond Hettinger60eca932003-02-09 06:40:58 +000036Some tools were omitted from the module because they offered no
37advantage over their pure python counterparts or because their behavior
38was too surprising.
Raymond Hettinger96ef8112003-02-01 00:10:11 +000039
Raymond Hettinger60eca932003-02-09 06:40:58 +000040For instance, SML provides a tool: \code{cycle(\var{seq})} which
41loops over the sequence elements and then starts again when the
42sequence is exhausted. The surprising behavior is the need for
43significant auxiliary storage (which is unusual for an iterator).
44If needed, the tool is readily constructible using pure Python.
Raymond Hettinger96ef8112003-02-01 00:10:11 +000045
Raymond Hettinger60eca932003-02-09 06:40:58 +000046Other tools are being considered for inclusion in future versions of the
47module. For instance, the function
48\function{chain(\var{it0}, \var{it1}, ...})} would return elements from
49the first iterator until it was exhausted and then move on to each
50successive iterator. The module author welcomes suggestions for other
51basic building blocks.
Raymond Hettinger96ef8112003-02-01 00:10:11 +000052
53\begin{seealso}
54 \seetext{The Standard ML Basis Library,
55 \citetitle[http://www.standardml.org/Basis/]
56 {The Standard ML Basis Library}.}
57
58 \seetext{Haskell, A Purely Functional Language,
59 \citetitle[http://www.haskell.org/definition/]
60 {Definition of Haskell and the Standard Libraries}.}
61\end{seealso}
62
63
64\subsection{Itertool functions \label{itertools-functions}}
65
66The following module functions all construct and return iterators.
67Some provide streams of infinite length, so they should only be accessed
68by functions or loops that truncate the stream.
69
70\begin{funcdesc}{count}{\optional{n}}
71 Make an iterator that returns consecutive integers starting with \var{n}.
72 Does not currently support python long integers. Often used as an
73 argument to \function{imap()} to generate consecutive data points.
74 Also, used in \function{izip()} to add sequence numbers. Equivalent to:
75
76 \begin{verbatim}
77 def count(n=0):
78 cnt = n
79 while True:
80 yield cnt
81 cnt += 1
82 \end{verbatim}
Raymond Hettinger2012f172003-02-07 05:32:58 +000083
84 Note, \function{count()} does not check for overflow and will return
85 negative numbers after exceeding \code{sys.maxint}. This behavior
86 may change in the future.
Raymond Hettinger96ef8112003-02-01 00:10:11 +000087\end{funcdesc}
88
89\begin{funcdesc}{dropwhile}{predicate, iterable}
90 Make an iterator that drops elements from the iterable as long as
91 the predicate is true; afterwards, returns every element. Note,
92 the iterator does not produce \emph{any} output until the predicate
93 is true, so it may have a lengthy start-up time. Equivalent to:
94
95 \begin{verbatim}
96 def dropwhile(predicate, iterable):
97 iterable = iter(iterable)
98 while True:
99 x = iterable.next()
100 if predicate(x): continue # drop when predicate is true
101 yield x
102 break
103 while True:
104 yield iterable.next()
105 \end{verbatim}
106\end{funcdesc}
107
Raymond Hettinger60eca932003-02-09 06:40:58 +0000108\begin{funcdesc}{ifilter}{predicate, iterable}
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000109 Make an iterator that filters elements from iterable returning only
Raymond Hettinger60eca932003-02-09 06:40:58 +0000110 those for which the predicate is \code{True}.
111 If \var{predicate} is \code{None}, return the items that are true.
112 Equivalent to:
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000113
114 \begin{verbatim}
Raymond Hettinger60eca932003-02-09 06:40:58 +0000115 def ifilter(predicate, iterable):
116 if predicate is None:
117 def predicate(x):
118 return x
119 for x in iterable:
120 if predicate(x):
121 yield x
122 \end{verbatim}
123\end{funcdesc}
124
125\begin{funcdesc}{ifilterfalse}{predicate, iterable}
126 Make an iterator that filters elements from iterable returning only
127 those for which the predicate is \code{False}.
128 If \var{predicate} is \code{None}, return the items that are false.
129 Equivalent to:
130
131 \begin{verbatim}
132 def ifilterfalse(predicate, iterable):
133 if predicate is None:
134 def predicate(x):
135 return x
136 for x in iterable:
137 if not predicate(x):
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000138 yield x
139 \end{verbatim}
140\end{funcdesc}
141
142\begin{funcdesc}{imap}{function, *iterables}
143 Make an iterator that computes the function using arguments from
144 each of the iterables. If \var{function} is set to \code{None}, then
145 \function{imap()} returns the arguments as a tuple. Like
146 \function{map()} but stops when the shortest iterable is exhausted
147 instead of filling in \code{None} for shorter iterables. The reason
148 for the difference is that infinite iterator arguments are typically
149 an error for \function{map()} (because the output is fully evaluated)
150 but represent a common and useful way of supplying arguments to
151 \function{imap()}.
152 Equivalent to:
153
154 \begin{verbatim}
155 def imap(function, *iterables):
156 iterables = map(iter, iterables)
157 while True:
158 args = [i.next() for i in iterables]
159 if function is None:
160 yield tuple(args)
161 else:
162 yield function(*args)
163 \end{verbatim}
164\end{funcdesc}
165
166\begin{funcdesc}{islice}{iterable, \optional{start,} stop \optional{, step}}
167 Make an iterator that returns selected elements from the iterable.
168 If \var{start} is non-zero, then elements from the iterable are skipped
169 until start is reached. Afterward, elements are returned consecutively
170 unless \var{step} is set higher than one which results in items being
171 skipped. If \var{stop} is specified, then iteration stops at the
172 specified element position; otherwise, it continues indefinitely or
173 until the iterable is exhausted. Unlike regular slicing,
174 \function{islice()} does not support negative values for \var{start},
175 \var{stop}, or \var{step}. Can be used to extract related fields
176 from data where the internal structure has been flattened (for
177 example, a multi-line report may list a name field on every
178 third line). Equivalent to:
179
180 \begin{verbatim}
181 def islice(iterable, *args):
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000182 s = slice(*args)
183 next = s.start or 0
184 stop = s.stop
185 step = s.step or 1
Raymond Hettinger60eca932003-02-09 06:40:58 +0000186 for cnt, element in enumerate(iterable):
187 if cnt < next:
188 continue
189 if cnt >= stop:
190 break
191 yield element
192 next += step
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000193 \end{verbatim}
194\end{funcdesc}
195
196\begin{funcdesc}{izip}{*iterables}
197 Make an iterator that aggregates elements from each of the iterables.
198 Like \function{zip()} except that it returns an iterator instead of
199 a list. Used for lock-step iteration over several iterables at a
200 time. Equivalent to:
201
202 \begin{verbatim}
203 def izip(*iterables):
204 iterables = map(iter, iterables)
205 while True:
206 result = [i.next() for i in iterables]
207 yield tuple(result)
208 \end{verbatim}
209\end{funcdesc}
210
211\begin{funcdesc}{repeat}{obj}
212 Make an iterator that returns \var{obj} over and over again.
213 Used as argument to \function{imap()} for invariant parameters
214 to the called function. Also used with function{izip()} to create
215 an invariant part of a tuple record. Equivalent to:
216
217 \begin{verbatim}
218 def repeat(x):
219 while True:
220 yield x
221 \end{verbatim}
222\end{funcdesc}
223
224\begin{funcdesc}{starmap}{function, iterable}
225 Make an iterator that computes the function using arguments tuples
226 obtained from the iterable. Used instead of \function{imap()} when
227 argument parameters are already grouped in tuples from a single iterable
228 (the data has been ``pre-zipped''). The difference between
229 \function{imap()} and \function{starmap} parallels the distinction
230 between \code{function(a,b)} and \code{function(*c)}.
231 Equivalent to:
232
233 \begin{verbatim}
234 def starmap(function, iterable):
235 iterable = iter(iterable)
236 while True:
237 yield function(*iterable.next())
238 \end{verbatim}
239\end{funcdesc}
240
241\begin{funcdesc}{takewhile}{predicate, iterable}
242 Make an iterator that returns elements from the iterable as long as
243 the predicate is true. Equivalent to:
244
245 \begin{verbatim}
246 def takewhile(predicate, iterable):
247 iterable = iter(iterable)
248 while True:
249 x = iterable.next()
250 if predicate(x):
251 yield x
252 else:
253 break
254 \end{verbatim}
255\end{funcdesc}
256
257\begin{funcdesc}{times}{n, \optional{object}}
258 Make an iterator that returns \var{object} \var{n} times.
259 \var{object} defaults to \code{None}. Used for looping a specific
260 number of times without creating a number object on each pass.
261 Equivalent to:
262
263 \begin{verbatim}
264 def times(n, object=None):
265 if n<0 : raise ValueError
266 for i in xrange(n):
267 yield object
268 \end{verbatim}
269\end{funcdesc}
270
271
272\subsection{Examples \label{itertools-example}}
273
274The following examples show common uses for each tool and
275demonstrate ways they can be combined.
276
277\begin{verbatim}
278>>> for i in times(3):
279... print "Hello"
280...
281Hello
282Hello
283Hello
284
285>>> amounts = [120.15, 764.05, 823.14]
286>>> for checknum, amount in izip(count(1200), amounts):
287... print 'Check %d is for $%.2f' % (checknum, amount)
288...
289Check 1200 is for $120.15
290Check 1201 is for $764.05
291Check 1202 is for $823.14
292
293>>> import operator
294>>> for cube in imap(operator.pow, xrange(1,4), repeat(3)):
295... print cube
296...
2971
2988
29927
300
301>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura',
302 '', 'martin', '', 'walter', '', 'samuele']
303>>> for name in islice(reportlines, 3, len(reportlines), 2):
304... print name.title()
305...
306Alex
307Laura
308Martin
309Walter
310Samuele
311
312\end{verbatim}
313
314This section has further examples of how itertools can be combined.
315Note that \function{enumerate()} and \method{iteritems()} already
316have highly efficient implementations in Python. They are only
317included here to illustrate how higher level tools can be created
318from building blocks.
319
320\begin{verbatim}
321>>> def enumerate(iterable):
322... return izip(count(), iterable)
323
324>>> def tabulate(function):
325... "Return function(0), function(1), ..."
326... return imap(function, count())
327
328>>> def iteritems(mapping):
329... return izip(mapping.iterkeys(), mapping.itervalues())
330
331>>> def nth(iterable, n):
332... "Returns the nth item"
Raymond Hettinger60eca932003-02-09 06:40:58 +0000333... return list(islice(iterable, n, n+1))
334
335>>> def all(pred, seq):
336... "Returns True if pred(x) is True for every element in the iterable"
337... return not nth(ifilterfalse(pred, seq), 0)
338
339>>> def some(pred, seq):
340... "Returns True if pred(x) is True at least one element in the iterable"
341... return bool(nth(ifilter(pred, seq), 0))
342
343>>> def no(pred, seq):
344... "Returns True if pred(x) is False for every element in the iterable"
345... return not nth(ifilter(pred, seq), 0)
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000346
347\end{verbatim}