blob: 7baa5e10de65137e3632f93f68fbe045f6098045 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`itertools` --- Functions creating iterators for efficient looping
2=======================================================================
3
4.. module:: itertools
5 :synopsis: Functions creating iterators for efficient looping.
6.. moduleauthor:: Raymond Hettinger <python@rcn.com>
7.. sectionauthor:: Raymond Hettinger <python@rcn.com>
8
9
Christian Heimesfe337bf2008-03-23 21:54:12 +000010.. testsetup::
11
12 from itertools import *
13
14
Raymond Hettingerf76b9202009-02-17 20:00:59 +000015This module implements a number of :term:`iterator` building blocks inspired
16by constructs from APL, Haskell, and SML. Each has been recast in a form
17suitable for Python.
Georg Brandl116aa622007-08-15 14:28:22 +000018
19The module standardizes a core set of fast, memory efficient tools that are
Raymond Hettingerf76b9202009-02-17 20:00:59 +000020useful by themselves or in combination. Together, they form an "iterator
21algebra" making it possible to construct specialized tools succinctly and
22efficiently in pure Python.
Georg Brandl116aa622007-08-15 14:28:22 +000023
24For instance, SML provides a tabulation tool: ``tabulate(f)`` which produces a
Raymond Hettingera6c60372008-03-13 01:26:19 +000025sequence ``f(0), f(1), ...``. But, this effect can be achieved in Python
26by combining :func:`map` and :func:`count` to form ``map(f, count())``.
Georg Brandl116aa622007-08-15 14:28:22 +000027
Raymond Hettinger2c109ab2009-03-12 00:29:44 +000028These tools and their built-in counterparts also work well with the high-speed
29functions in the :mod:`operator` module. For example, the multiplication
30operator can be mapped across two vectors to form an efficient dot-product:
31``sum(map(operator.mul, vector1, vector2))``.
Georg Brandl116aa622007-08-15 14:28:22 +000032
Georg Brandl116aa622007-08-15 14:28:22 +000033
Raymond Hettingerf76b9202009-02-17 20:00:59 +000034**Infinite Iterators:**
Georg Brandl116aa622007-08-15 14:28:22 +000035
Raymond Hettinger5bfd8ce2009-04-10 19:02:36 +000036================== ================= ================================================= =========================================
37Iterator Arguments Results Example
38================== ================= ================================================= =========================================
39:func:`count` start, [step] start, start+step, start+2*step, ... ``count(10) --> 10 11 12 13 14 ...``
40:func:`cycle` p p0, p1, ... plast, p0, p1, ... ``cycle('ABCD') --> A B C D A B C D ...``
41:func:`repeat` elem [,n] elem, elem, elem, ... endlessly or up to n times ``repeat(10, 3) --> 10 10 10``
42================== ================= ================================================= =========================================
Georg Brandl116aa622007-08-15 14:28:22 +000043
Raymond Hettingerf76b9202009-02-17 20:00:59 +000044**Iterators terminating on the shortest input sequence:**
45
Raymond Hettinger5bfd8ce2009-04-10 19:02:36 +000046==================== ============================ ================================================= =============================================================
47Iterator Arguments Results Example
48==================== ============================ ================================================= =============================================================
49:func:`chain` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') --> A B C D E F``
50:func:`compress` data, selectors (d[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F``
51:func:`dropwhile` pred, seq seq[n], seq[n+1], starting when pred fails ``dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1``
52:func:`filterfalse` pred, seq elements of seq where pred(elem) is False ``ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8``
53:func:`groupby` iterable[, keyfunc] sub-iterators grouped by value of keyfunc(v)
54:func:`islice` seq, [start,] stop [, step] elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) --> C D E F G``
55:func:`starmap` func, seq func(\*seq[0]), func(\*seq[1]), ... ``starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000``
56:func:`takewhile` pred, seq seq[0], seq[1], until pred fails ``takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4``
57:func:`tee` it, n it1, it2 , ... itn splits one iterator into n
58:func:`zip_longest` p, q, ... (p[0], q[0]), (p[1], q[1]), ... ``izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-``
59==================== ============================ ================================================= =============================================================
Raymond Hettingerf76b9202009-02-17 20:00:59 +000060
61**Combinatoric generators:**
62
Raymond Hettinger7f587cd2009-04-10 19:43:50 +000063============================================== ==================== =============================================================
64Iterator Arguments Results
65============================================== ==================== =============================================================
66:func:`product` p, q, ... [repeat=1] cartesian product, equivalent to a nested for-loop
67:func:`permutations` p[, r] r-length tuples, all possible orderings, no repeated elements
68:func:`combinations` p[, r] r-length tuples, in sorted order, no repeated elements
69:func:`combinations_with_replacement` p[, r] r-length tuples, in sorted order, with repeated elements
70|
71``product('ABCD', repeat=2)`` ``AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD``
72``permutations('ABCD', 2)`` ``AB AC AD BA BC BD CA CB CD DA DB DC``
73``combinations('ABCD', 2)`` ``AB AC AD BC BD CD``
74``combinations_with_replacement('ABCD', 2)`` ``AA AB AC AD BB BC BD CC CD DD``
75============================================== ==================== =============================================================
Georg Brandl116aa622007-08-15 14:28:22 +000076
77
78.. _itertools-functions:
79
80Itertool functions
81------------------
82
83The following module functions all construct and return iterators. Some provide
84streams of infinite length, so they should only be accessed by functions or
85loops that truncate the stream.
86
87
88.. function:: chain(*iterables)
89
90 Make an iterator that returns elements from the first iterable until it is
91 exhausted, then proceeds to the next iterable, until all of the iterables are
92 exhausted. Used for treating consecutive sequences as a single sequence.
93 Equivalent to::
94
95 def chain(*iterables):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +000096 # chain('ABC', 'DEF') --> A B C D E F
Georg Brandl116aa622007-08-15 14:28:22 +000097 for it in iterables:
98 for element in it:
99 yield element
100
101
Christian Heimes70e7ea22008-02-28 20:02:27 +0000102.. function:: itertools.chain.from_iterable(iterable)
103
Georg Brandl48310cd2009-01-03 21:18:54 +0000104 Alternate constructor for :func:`chain`. Gets chained inputs from a
Christian Heimes70e7ea22008-02-28 20:02:27 +0000105 single iterable argument that is evaluated lazily. Equivalent to::
106
107 @classmethod
108 def from_iterable(iterables):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000109 # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
Christian Heimes70e7ea22008-02-28 20:02:27 +0000110 for it in iterables:
111 for element in it:
112 yield element
113
Christian Heimes78644762008-03-04 23:39:23 +0000114
Christian Heimes836baa52008-02-26 08:18:30 +0000115.. function:: combinations(iterable, r)
116
Christian Heimesdae2a892008-04-19 00:55:37 +0000117 Return *r* length subsequences of elements from the input *iterable*.
Christian Heimes836baa52008-02-26 08:18:30 +0000118
Georg Brandl48310cd2009-01-03 21:18:54 +0000119 Combinations are emitted in lexicographic sort order. So, if the
Christian Heimes836baa52008-02-26 08:18:30 +0000120 input *iterable* is sorted, the combination tuples will be produced
Georg Brandl48310cd2009-01-03 21:18:54 +0000121 in sorted order.
Christian Heimes836baa52008-02-26 08:18:30 +0000122
123 Elements are treated as unique based on their position, not on their
124 value. So if the input elements are unique, there will be no repeat
Christian Heimes70e7ea22008-02-28 20:02:27 +0000125 values in each combination.
Christian Heimes836baa52008-02-26 08:18:30 +0000126
Christian Heimes836baa52008-02-26 08:18:30 +0000127 Equivalent to::
128
129 def combinations(iterable, r):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000130 # combinations('ABCD', 2) --> AB AC AD BC BD CD
131 # combinations(range(4), 3) --> 012 013 023 123
Christian Heimes836baa52008-02-26 08:18:30 +0000132 pool = tuple(iterable)
Christian Heimes380f7f22008-02-28 11:19:05 +0000133 n = len(pool)
Raymond Hettinger5bad41e2009-01-08 21:01:54 +0000134 if r > n:
135 return
136 indices = list(range(r))
Christian Heimesb558a2e2008-03-02 22:46:37 +0000137 yield tuple(pool[i] for i in indices)
Raymond Hettingercf984ce2009-02-18 20:56:51 +0000138 while True:
Christian Heimes380f7f22008-02-28 11:19:05 +0000139 for i in reversed(range(r)):
Christian Heimesb558a2e2008-03-02 22:46:37 +0000140 if indices[i] != i + n - r:
Christian Heimes836baa52008-02-26 08:18:30 +0000141 break
Christian Heimes380f7f22008-02-28 11:19:05 +0000142 else:
143 return
Christian Heimesb558a2e2008-03-02 22:46:37 +0000144 indices[i] += 1
Christian Heimes380f7f22008-02-28 11:19:05 +0000145 for j in range(i+1, r):
Christian Heimesb558a2e2008-03-02 22:46:37 +0000146 indices[j] = indices[j-1] + 1
147 yield tuple(pool[i] for i in indices)
Christian Heimes836baa52008-02-26 08:18:30 +0000148
Christian Heimes78644762008-03-04 23:39:23 +0000149 The code for :func:`combinations` can be also expressed as a subsequence
150 of :func:`permutations` after filtering entries where the elements are not
151 in sorted order (according to their position in the input pool)::
152
153 def combinations(iterable, r):
154 pool = tuple(iterable)
155 n = len(pool)
156 for indices in permutations(range(n), r):
157 if sorted(indices) == list(indices):
158 yield tuple(pool[i] for i in indices)
159
Raymond Hettinger5bad41e2009-01-08 21:01:54 +0000160 The number of items returned is ``n! / r! / (n-r)!`` when ``0 <= r <= n``
161 or zero when ``r > n``.
Christian Heimes836baa52008-02-26 08:18:30 +0000162
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000163.. function:: combinations_with_replacement(iterable, r)
164
165 Return *r* length subsequences of elements from the input *iterable*
166 allowing individual elements to be repeated more than once.
167
168 Combinations are emitted in lexicographic sort order. So, if the
169 input *iterable* is sorted, the combination tuples will be produced
170 in sorted order.
171
172 Elements are treated as unique based on their position, not on their
173 value. So if the input elements are unique, the generated combinations
174 will also be unique.
175
176 Equivalent to::
177
178 def combinations_with_replacement(iterable, r):
179 # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
180 pool = tuple(iterable)
181 n = len(pool)
182 if not n and r:
183 return
184 indices = [0] * r
185 yield tuple(pool[i] for i in indices)
Raymond Hettingercf984ce2009-02-18 20:56:51 +0000186 while True:
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000187 for i in reversed(range(r)):
188 if indices[i] != n - 1:
189 break
190 else:
191 return
192 indices[i:] = [indices[i] + 1] * (r - i)
193 yield tuple(pool[i] for i in indices)
194
195 The code for :func:`combinations_with_replacement` can be also expressed as
196 a subsequence of :func:`product` after filtering entries where the elements
197 are not in sorted order (according to their position in the input pool)::
198
199 def combinations_with_replacement(iterable, r):
200 pool = tuple(iterable)
201 n = len(pool)
202 for indices in product(range(n), repeat=r):
203 if sorted(indices) == list(indices):
204 yield tuple(pool[i] for i in indices)
205
206 The number of items returned is ``(n+r-1)! / r! / (n-1)!`` when ``n > 0``.
207
Raymond Hettinger30729212009-02-12 06:28:27 +0000208 .. versionadded:: 3.1
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000209
Raymond Hettinger6b3b0fc2009-01-26 02:56:58 +0000210.. function:: compress(data, selectors)
211
212 Make an iterator that filters elements from *data* returning only those that
213 have a corresponding element in *selectors* that evaluates to ``True``.
Benjamin Petersond23f8222009-04-05 19:13:16 +0000214 Stops when either the *data* or *selectors* iterables has been exhausted.
Raymond Hettinger6b3b0fc2009-01-26 02:56:58 +0000215 Equivalent to::
216
217 def compress(data, selectors):
218 # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
219 return (d for d, s in zip(data, selectors) if s)
220
Raymond Hettinger30729212009-02-12 06:28:27 +0000221 .. versionadded:: 3.1
Raymond Hettinger6b3b0fc2009-01-26 02:56:58 +0000222
223
Raymond Hettinger9e8dbbc2009-02-14 04:21:49 +0000224.. function:: count(start=0, step=1)
Georg Brandl116aa622007-08-15 14:28:22 +0000225
Raymond Hettinger30729212009-02-12 06:28:27 +0000226 Make an iterator that returns evenly spaced values starting with *n*. Often
227 used as an argument to :func:`map` to generate consecutive data points.
228 Also, used with :func:`zip` to add sequence numbers. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000229
Raymond Hettinger9e8dbbc2009-02-14 04:21:49 +0000230 def count(start=0, step=1):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000231 # count(10) --> 10 11 12 13 14 ...
Raymond Hettinger30729212009-02-12 06:28:27 +0000232 # count(2.5, 0.5) -> 3.5 3.0 4.5 ...
Raymond Hettinger9e8dbbc2009-02-14 04:21:49 +0000233 n = start
Georg Brandl116aa622007-08-15 14:28:22 +0000234 while True:
235 yield n
Raymond Hettinger30729212009-02-12 06:28:27 +0000236 n += step
Georg Brandl116aa622007-08-15 14:28:22 +0000237
Raymond Hettinger30729212009-02-12 06:28:27 +0000238 .. versionchanged:: 3.1
239 added *step* argument and allowed non-integer arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000240
241.. function:: cycle(iterable)
242
243 Make an iterator returning elements from the iterable and saving a copy of each.
244 When the iterable is exhausted, return elements from the saved copy. Repeats
245 indefinitely. Equivalent to::
246
247 def cycle(iterable):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000248 # cycle('ABCD') --> A B C D A B C D A B C D ...
Georg Brandl116aa622007-08-15 14:28:22 +0000249 saved = []
250 for element in iterable:
251 yield element
252 saved.append(element)
253 while saved:
254 for element in saved:
255 yield element
256
257 Note, this member of the toolkit may require significant auxiliary storage
258 (depending on the length of the iterable).
259
260
261.. function:: dropwhile(predicate, iterable)
262
263 Make an iterator that drops elements from the iterable as long as the predicate
264 is true; afterwards, returns every element. Note, the iterator does not produce
265 *any* output until the predicate first becomes false, so it may have a lengthy
266 start-up time. Equivalent to::
267
268 def dropwhile(predicate, iterable):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000269 # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
Georg Brandl116aa622007-08-15 14:28:22 +0000270 iterable = iter(iterable)
271 for x in iterable:
272 if not predicate(x):
273 yield x
274 break
275 for x in iterable:
276 yield x
277
Raymond Hettinger749761e2009-01-27 04:42:48 +0000278.. function:: filterfalse(predicate, iterable)
279
280 Make an iterator that filters elements from iterable returning only those for
281 which the predicate is ``False``. If *predicate* is ``None``, return the items
282 that are false. Equivalent to::
283
284 def filterfalse(predicate, iterable):
285 # filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
286 if predicate is None:
287 predicate = bool
288 for x in iterable:
289 if not predicate(x):
290 yield x
291
Georg Brandl116aa622007-08-15 14:28:22 +0000292
Georg Brandl3dd33882009-06-01 17:35:27 +0000293.. function:: groupby(iterable, key=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000294
295 Make an iterator that returns consecutive keys and groups from the *iterable*.
296 The *key* is a function computing a key value for each element. If not
297 specified or is ``None``, *key* defaults to an identity function and returns
298 the element unchanged. Generally, the iterable needs to already be sorted on
299 the same key function.
300
301 The operation of :func:`groupby` is similar to the ``uniq`` filter in Unix. It
302 generates a break or new group every time the value of the key function changes
303 (which is why it is usually necessary to have sorted the data using the same key
304 function). That behavior differs from SQL's GROUP BY which aggregates common
305 elements regardless of their input order.
306
307 The returned group is itself an iterator that shares the underlying iterable
308 with :func:`groupby`. Because the source is shared, when the :func:`groupby`
309 object is advanced, the previous group is no longer visible. So, if that data
310 is needed later, it should be stored as a list::
311
312 groups = []
313 uniquekeys = []
314 data = sorted(data, key=keyfunc)
315 for k, g in groupby(data, keyfunc):
316 groups.append(list(g)) # Store group iterator as a list
317 uniquekeys.append(k)
318
319 :func:`groupby` is equivalent to::
320
321 class groupby(object):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000322 # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
Raymond Hettingerd04fa312009-02-04 19:45:13 +0000323 # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
Georg Brandl116aa622007-08-15 14:28:22 +0000324 def __init__(self, iterable, key=None):
325 if key is None:
326 key = lambda x: x
327 self.keyfunc = key
328 self.it = iter(iterable)
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000329 self.tgtkey = self.currkey = self.currvalue = object()
Georg Brandl116aa622007-08-15 14:28:22 +0000330 def __iter__(self):
331 return self
332 def __next__(self):
333 while self.currkey == self.tgtkey:
Raymond Hettinger21315ba2009-02-23 19:38:09 +0000334 self.currvalue = next(self.it) # Exit on StopIteration
Georg Brandl116aa622007-08-15 14:28:22 +0000335 self.currkey = self.keyfunc(self.currvalue)
336 self.tgtkey = self.currkey
337 return (self.currkey, self._grouper(self.tgtkey))
338 def _grouper(self, tgtkey):
339 while self.currkey == tgtkey:
340 yield self.currvalue
Raymond Hettinger21315ba2009-02-23 19:38:09 +0000341 self.currvalue = next(self.it) # Exit on StopIteration
Georg Brandl116aa622007-08-15 14:28:22 +0000342 self.currkey = self.keyfunc(self.currvalue)
343
Georg Brandl116aa622007-08-15 14:28:22 +0000344
Georg Brandl116aa622007-08-15 14:28:22 +0000345.. function:: islice(iterable, [start,] stop [, step])
346
347 Make an iterator that returns selected elements from the iterable. If *start* is
348 non-zero, then elements from the iterable are skipped until start is reached.
349 Afterward, elements are returned consecutively unless *step* is set higher than
350 one which results in items being skipped. If *stop* is ``None``, then iteration
351 continues until the iterator is exhausted, if at all; otherwise, it stops at the
352 specified position. Unlike regular slicing, :func:`islice` does not support
353 negative values for *start*, *stop*, or *step*. Can be used to extract related
354 fields from data where the internal structure has been flattened (for example, a
355 multi-line report may list a name field on every third line). Equivalent to::
356
357 def islice(iterable, *args):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000358 # islice('ABCDEFG', 2) --> A B
359 # islice('ABCDEFG', 2, 4) --> C D
360 # islice('ABCDEFG', 2, None) --> C D E F G
361 # islice('ABCDEFG', 0, None, 2) --> A C E G
Georg Brandl116aa622007-08-15 14:28:22 +0000362 s = slice(*args)
Raymond Hettinger7c8494b2009-05-14 21:48:02 +0000363 it = iter(range(s.start or 0, s.stop or sys.maxsize, s.step or 1))
Georg Brandl116aa622007-08-15 14:28:22 +0000364 nexti = next(it)
365 for i, element in enumerate(iterable):
366 if i == nexti:
367 yield element
368 nexti = next(it)
369
370 If *start* is ``None``, then iteration starts at zero. If *step* is ``None``,
371 then the step defaults to one.
372
Georg Brandl116aa622007-08-15 14:28:22 +0000373
Georg Brandl3dd33882009-06-01 17:35:27 +0000374.. function:: permutations(iterable, r=None)
Christian Heimes70e7ea22008-02-28 20:02:27 +0000375
376 Return successive *r* length permutations of elements in the *iterable*.
377
378 If *r* is not specified or is ``None``, then *r* defaults to the length
Georg Brandl48310cd2009-01-03 21:18:54 +0000379 of the *iterable* and all possible full-length permutations
Christian Heimes70e7ea22008-02-28 20:02:27 +0000380 are generated.
381
Georg Brandl48310cd2009-01-03 21:18:54 +0000382 Permutations are emitted in lexicographic sort order. So, if the
Christian Heimes70e7ea22008-02-28 20:02:27 +0000383 input *iterable* is sorted, the permutation tuples will be produced
Georg Brandl48310cd2009-01-03 21:18:54 +0000384 in sorted order.
Christian Heimes70e7ea22008-02-28 20:02:27 +0000385
386 Elements are treated as unique based on their position, not on their
387 value. So if the input elements are unique, there will be no repeat
388 values in each permutation.
389
Christian Heimesb558a2e2008-03-02 22:46:37 +0000390 Equivalent to::
391
392 def permutations(iterable, r=None):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000393 # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
394 # permutations(range(3)) --> 012 021 102 120 201 210
Christian Heimesb558a2e2008-03-02 22:46:37 +0000395 pool = tuple(iterable)
396 n = len(pool)
397 r = n if r is None else r
Raymond Hettinger5bad41e2009-01-08 21:01:54 +0000398 if r > n:
399 return
400 indices = list(range(n))
Christian Heimesfe337bf2008-03-23 21:54:12 +0000401 cycles = range(n, n-r, -1)
Christian Heimesb558a2e2008-03-02 22:46:37 +0000402 yield tuple(pool[i] for i in indices[:r])
403 while n:
404 for i in reversed(range(r)):
405 cycles[i] -= 1
406 if cycles[i] == 0:
407 indices[i:] = indices[i+1:] + indices[i:i+1]
408 cycles[i] = n - i
409 else:
410 j = cycles[i]
411 indices[i], indices[-j] = indices[-j], indices[i]
412 yield tuple(pool[i] for i in indices[:r])
413 break
414 else:
415 return
Christian Heimes70e7ea22008-02-28 20:02:27 +0000416
Georg Brandl48310cd2009-01-03 21:18:54 +0000417 The code for :func:`permutations` can be also expressed as a subsequence of
Christian Heimes78644762008-03-04 23:39:23 +0000418 :func:`product`, filtered to exclude entries with repeated elements (those
419 from the same position in the input pool)::
420
421 def permutations(iterable, r=None):
422 pool = tuple(iterable)
423 n = len(pool)
424 r = n if r is None else r
425 for indices in product(range(n), repeat=r):
426 if len(set(indices)) == r:
427 yield tuple(pool[i] for i in indices)
428
Raymond Hettinger5bad41e2009-01-08 21:01:54 +0000429 The number of items returned is ``n! / (n-r)!`` when ``0 <= r <= n``
430 or zero when ``r > n``.
Christian Heimes70e7ea22008-02-28 20:02:27 +0000431
Georg Brandl3dd33882009-06-01 17:35:27 +0000432.. function:: product(*iterables, repeat=1)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000433
434 Cartesian product of input iterables.
435
436 Equivalent to nested for-loops in a generator expression. For example,
437 ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``.
438
Christian Heimesdae2a892008-04-19 00:55:37 +0000439 The nested loops cycle like an odometer with the rightmost element advancing
440 on every iteration. This pattern creates a lexicographic ordering so that if
441 the input's iterables are sorted, the product tuples are emitted in sorted
442 order.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000443
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000444 To compute the product of an iterable with itself, specify the number of
445 repetitions with the optional *repeat* keyword argument. For example,
446 ``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``.
447
Christian Heimes78644762008-03-04 23:39:23 +0000448 This function is equivalent to the following code, except that the
449 actual implementation does not build up intermediate results in memory::
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000450
Raymond Hettingera137e1f2008-03-13 02:43:14 +0000451 def product(*args, repeat=1):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000452 # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
453 # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
Raymond Hettingera137e1f2008-03-13 02:43:14 +0000454 pools = map(tuple, args) * repeat
Christian Heimes78644762008-03-04 23:39:23 +0000455 result = [[]]
456 for pool in pools:
457 result = [x+[y] for x in result for y in pool]
458 for prod in result:
459 yield tuple(prod)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000460
461
Raymond Hettingerd75ad442009-06-01 19:16:52 +0000462.. function:: repeat(object[, times])
Georg Brandl116aa622007-08-15 14:28:22 +0000463
464 Make an iterator that returns *object* over and over again. Runs indefinitely
Raymond Hettingera6c60372008-03-13 01:26:19 +0000465 unless the *times* argument is specified. Used as argument to :func:`map` for
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000466 invariant parameters to the called function. Also used with :func:`zip` to
Georg Brandl116aa622007-08-15 14:28:22 +0000467 create an invariant part of a tuple record. Equivalent to::
468
469 def repeat(object, times=None):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000470 # repeat(10, 3) --> 10 10 10
Georg Brandl116aa622007-08-15 14:28:22 +0000471 if times is None:
472 while True:
473 yield object
474 else:
475 for i in range(times):
476 yield object
477
478
479.. function:: starmap(function, iterable)
480
Christian Heimes679db4a2008-01-18 09:56:22 +0000481 Make an iterator that computes the function using arguments obtained from
Raymond Hettingera6c60372008-03-13 01:26:19 +0000482 the iterable. Used instead of :func:`map` when argument parameters are already
Georg Brandl116aa622007-08-15 14:28:22 +0000483 grouped in tuples from a single iterable (the data has been "pre-zipped"). The
Raymond Hettingera6c60372008-03-13 01:26:19 +0000484 difference between :func:`map` and :func:`starmap` parallels the distinction
Georg Brandl116aa622007-08-15 14:28:22 +0000485 between ``function(a,b)`` and ``function(*c)``. Equivalent to::
486
487 def starmap(function, iterable):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000488 # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
Christian Heimes679db4a2008-01-18 09:56:22 +0000489 for args in iterable:
490 yield function(*args)
491
Georg Brandl116aa622007-08-15 14:28:22 +0000492
493.. function:: takewhile(predicate, iterable)
494
495 Make an iterator that returns elements from the iterable as long as the
496 predicate is true. Equivalent to::
497
498 def takewhile(predicate, iterable):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000499 # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
Georg Brandl116aa622007-08-15 14:28:22 +0000500 for x in iterable:
501 if predicate(x):
502 yield x
503 else:
504 break
505
506
Georg Brandl3dd33882009-06-01 17:35:27 +0000507.. function:: tee(iterable, n=2)
Georg Brandl116aa622007-08-15 14:28:22 +0000508
Raymond Hettingercf984ce2009-02-18 20:56:51 +0000509 Return *n* independent iterators from a single iterable. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000510
Raymond Hettingercf984ce2009-02-18 20:56:51 +0000511 def tee(iterable, n=2):
512 it = iter(iterable)
513 deques = [collections.deque() for i in range(n)]
514 def gen(mydeque):
515 while True:
516 if not mydeque: # when the local deque is empty
517 newval = next(it) # fetch a new value and
518 for d in deques: # load it to all the deques
519 d.append(newval)
520 yield mydeque.popleft()
521 return tuple(gen(d) for d in deques)
Georg Brandl116aa622007-08-15 14:28:22 +0000522
Raymond Hettingercf984ce2009-02-18 20:56:51 +0000523 Once :func:`tee` has made a split, the original *iterable* should not be
524 used anywhere else; otherwise, the *iterable* could get advanced without
525 the tee objects being informed.
Georg Brandl116aa622007-08-15 14:28:22 +0000526
Raymond Hettingercf984ce2009-02-18 20:56:51 +0000527 This itertool may require significant auxiliary storage (depending on how
528 much temporary data needs to be stored). In general, if one iterator uses
529 most or all of the data before another iterator starts, it is faster to use
530 :func:`list` instead of :func:`tee`.
Georg Brandl116aa622007-08-15 14:28:22 +0000531
Georg Brandl116aa622007-08-15 14:28:22 +0000532
Georg Brandl3dd33882009-06-01 17:35:27 +0000533.. function:: zip_longest(*iterables, fillvalue=None)
Raymond Hettinger749761e2009-01-27 04:42:48 +0000534
535 Make an iterator that aggregates elements from each of the iterables. If the
536 iterables are of uneven length, missing values are filled-in with *fillvalue*.
537 Iteration continues until the longest iterable is exhausted. Equivalent to::
538
539 def zip_longest(*args, fillvalue=None):
540 # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
541 def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
542 yield counter() # yields the fillvalue, or raises IndexError
543 fillers = repeat(fillvalue)
544 iters = [chain(it, sentinel(), fillers) for it in args]
545 try:
546 for tup in zip(*iters):
547 yield tup
548 except IndexError:
549 pass
550
551 If one of the iterables is potentially infinite, then the :func:`zip_longest`
552 function should be wrapped with something that limits the number of calls
553 (for example :func:`islice` or :func:`takewhile`). If not specified,
554 *fillvalue* defaults to ``None``.
555
556
Georg Brandl116aa622007-08-15 14:28:22 +0000557.. _itertools-recipes:
558
559Recipes
560-------
561
562This section shows recipes for creating an extended toolset using the existing
563itertools as building blocks.
564
565The extended tools offer the same high performance as the underlying toolset.
566The superior memory performance is kept by processing elements one at a time
567rather than bringing the whole iterable into memory all at once. Code volume is
568kept small by linking the tools together in a functional style which helps
569eliminate temporary variables. High speed is retained by preferring
Georg Brandl9afde1c2007-11-01 20:32:30 +0000570"vectorized" building blocks over the use of for-loops and :term:`generator`\s
Christian Heimesfe337bf2008-03-23 21:54:12 +0000571which incur interpreter overhead.
572
573.. testcode::
Georg Brandl116aa622007-08-15 14:28:22 +0000574
Georg Brandl3dbca812008-07-23 16:10:53 +0000575 def take(n, iterable):
576 "Return first n items of the iterable as a list"
577 return list(islice(iterable, n))
Georg Brandl116aa622007-08-15 14:28:22 +0000578
Georg Brandl3dbca812008-07-23 16:10:53 +0000579 def enumerate(iterable, start=0):
580 return zip(count(start), iterable)
Georg Brandl116aa622007-08-15 14:28:22 +0000581
Georg Brandl3dbca812008-07-23 16:10:53 +0000582 def tabulate(function, start=0):
Georg Brandl116aa622007-08-15 14:28:22 +0000583 "Return function(0), function(1), ..."
Georg Brandl3dbca812008-07-23 16:10:53 +0000584 return map(function, count(start))
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000585
Raymond Hettingerfa007962009-03-09 11:55:25 +0000586 def consume(iterator, n):
587 "Advance the iterator n-steps ahead. If n is none, consume entirely."
588 collections.deque(islice(iterator, n), maxlen=0)
589
Raymond Hettingercdf8ba32009-02-19 04:45:07 +0000590 def nth(iterable, n, default=None):
591 "Returns the nth item or a default value"
592 return next(islice(iterable, n, None), default)
Georg Brandl116aa622007-08-15 14:28:22 +0000593
Georg Brandl3dbca812008-07-23 16:10:53 +0000594 def quantify(iterable, pred=bool):
595 "Count how many times the predicate is true"
596 return sum(map(pred, iterable))
Georg Brandl116aa622007-08-15 14:28:22 +0000597
Georg Brandl3dbca812008-07-23 16:10:53 +0000598 def padnone(iterable):
Georg Brandl116aa622007-08-15 14:28:22 +0000599 """Returns the sequence elements and then returns None indefinitely.
600
601 Useful for emulating the behavior of the built-in map() function.
602 """
Georg Brandl3dbca812008-07-23 16:10:53 +0000603 return chain(iterable, repeat(None))
Georg Brandl116aa622007-08-15 14:28:22 +0000604
Georg Brandl3dbca812008-07-23 16:10:53 +0000605 def ncycles(iterable, n):
Georg Brandl116aa622007-08-15 14:28:22 +0000606 "Returns the sequence elements n times"
Georg Brandl3dbca812008-07-23 16:10:53 +0000607 return chain.from_iterable(repeat(iterable, n))
Georg Brandl116aa622007-08-15 14:28:22 +0000608
609 def dotproduct(vec1, vec2):
Raymond Hettingera6c60372008-03-13 01:26:19 +0000610 return sum(map(operator.mul, vec1, vec2))
Georg Brandl116aa622007-08-15 14:28:22 +0000611
612 def flatten(listOfLists):
Christian Heimes70e7ea22008-02-28 20:02:27 +0000613 return list(chain.from_iterable(listOfLists))
Georg Brandl116aa622007-08-15 14:28:22 +0000614
615 def repeatfunc(func, times=None, *args):
616 """Repeat calls to func with specified arguments.
617
618 Example: repeatfunc(random.random)
619 """
620 if times is None:
621 return starmap(func, repeat(args))
Christian Heimes70e7ea22008-02-28 20:02:27 +0000622 return starmap(func, repeat(args, times))
Georg Brandl116aa622007-08-15 14:28:22 +0000623
624 def pairwise(iterable):
625 "s -> (s0,s1), (s1,s2), (s2, s3), ..."
626 a, b = tee(iterable)
Raymond Hettinger21315ba2009-02-23 19:38:09 +0000627 next(b, None)
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000628 return zip(a, b)
Georg Brandl116aa622007-08-15 14:28:22 +0000629
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000630 def grouper(n, iterable, fillvalue=None):
Raymond Hettingerf5a2e472008-07-30 07:37:37 +0000631 "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000632 args = [iter(iterable)] * n
Raymond Hettinger883d2762009-01-27 04:57:51 +0000633 return zip_longest(*args, fillvalue=fillvalue)
Georg Brandl116aa622007-08-15 14:28:22 +0000634
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000635 def roundrobin(*iterables):
Raymond Hettingerf5a2e472008-07-30 07:37:37 +0000636 "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
Christian Heimes70e7ea22008-02-28 20:02:27 +0000637 # Recipe credited to George Sakkis
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000638 pending = len(iterables)
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000639 nexts = cycle(iter(it).__next__ for it in iterables)
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000640 while pending:
641 try:
642 for next in nexts:
643 yield next()
644 except StopIteration:
645 pending -= 1
646 nexts = cycle(islice(nexts, pending))
Georg Brandl116aa622007-08-15 14:28:22 +0000647
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000648 def powerset(iterable):
Raymond Hettingerace67332009-01-26 02:23:50 +0000649 "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
650 s = list(iterable)
651 return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000652
Raymond Hettingerfd88ea72009-03-23 22:42:28 +0000653 def unique_everseen(iterable, key=None):
654 "List unique elements, preserving order. Remember all elements ever seen."
655 # unique_everseen('AAAABBBCCDAABBB') --> A B C D
656 # unique_everseen('ABBCcAD', str.lower) --> A B C D
657 seen = set()
658 seen_add = seen.add
659 if key is None:
660 for element in iterable:
661 if element not in seen:
662 seen_add(element)
663 yield element
664 else:
665 for element in iterable:
666 k = key(element)
667 if k not in seen:
668 seen_add(k)
669 yield element
Raymond Hettingerad9d96b2009-01-02 21:39:07 +0000670
Raymond Hettingerfd88ea72009-03-23 22:42:28 +0000671 def unique_justseen(iterable, key=None):
672 "List unique elements, preserving order. Remember only the element just seen."
673 # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
674 # unique_justseen('ABBCcAD', str.lower) --> A B C A D
675 return map(next, map(itemgetter(1), groupby(iterable, key)))