blob: 56eb452b0b0f603e59bfbc1aa69a6bea81c86a37 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`itertools` --- Functions creating iterators for efficient looping
2=======================================================================
3
4.. module:: itertools
Raymond Hettinger30c73622010-12-01 23:45:20 +00005 :synopsis: Functions creating iterators for efficient looping.
Georg Brandl116aa622007-08-15 14:28:22 +00006.. 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
Raymond Hettinger30c73622010-12-01 23:45:20 +000012 from itertools import *
Christian Heimesfe337bf2008-03-23 21:54:12 +000013
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
Ezio Melottib6605992010-01-21 20:57:24 +000025sequence ``f(0), f(1), ...``. The same effect can be achieved in Python
Raymond Hettingera6c60372008-03-13 01:26:19 +000026by 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==================== ============================ ================================================= =============================================================
Raymond Hettinger30c73622010-12-01 23:45:20 +000049:func:`accumulate` p[, start=0] p0, p0+p1, p0+p1+p2, ... ``accumulate([1,2,3,4,5]) --> 1 3 6 10 15``
Raymond Hettinger5bfd8ce2009-04-10 19:02:36 +000050:func:`chain` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') --> A B C D E F``
51: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``
52: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``
Georg Brandlc2da5ce2009-08-13 09:16:39 +000053:func:`filterfalse` pred, seq elements of seq where pred(elem) is False ``filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8``
Raymond Hettinger5bfd8ce2009-04-10 19:02:36 +000054:func:`groupby` iterable[, keyfunc] sub-iterators grouped by value of keyfunc(v)
55:func:`islice` seq, [start,] stop [, step] elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) --> C D E F G``
56:func:`starmap` func, seq func(\*seq[0]), func(\*seq[1]), ... ``starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000``
57:func:`takewhile` pred, seq seq[0], seq[1], until pred fails ``takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4``
58:func:`tee` it, n it1, it2 , ... itn splits one iterator into n
Georg Brandlc2da5ce2009-08-13 09:16:39 +000059:func:`zip_longest` p, q, ... (p[0], q[0]), (p[1], q[1]), ... ``zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-``
Raymond Hettinger5bfd8ce2009-04-10 19:02:36 +000060==================== ============================ ================================================= =============================================================
Raymond Hettingerf76b9202009-02-17 20:00:59 +000061
62**Combinatoric generators:**
63
Raymond Hettinger7f587cd2009-04-10 19:43:50 +000064============================================== ==================== =============================================================
65Iterator Arguments Results
66============================================== ==================== =============================================================
67:func:`product` p, q, ... [repeat=1] cartesian product, equivalent to a nested for-loop
68:func:`permutations` p[, r] r-length tuples, all possible orderings, no repeated elements
Raymond Hettinger36c3c022009-11-19 01:20:07 +000069:func:`combinations` p, r r-length tuples, in sorted order, no repeated elements
70:func:`combinations_with_replacement` p, r r-length tuples, in sorted order, with repeated elements
Raymond Hettinger7f587cd2009-04-10 19:43:50 +000071``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
Raymond Hettingeradb81462010-12-01 22:50:36 +000087.. function:: accumulate(iterable, start=0)
88
89 Make an iterator that returns accumulated sums plus the value of the *start*
90 parameter (which defaults to :const:`0`). Elements may be any addable type
91 including :class:`Decimal` or :class:`Fraction`. Equivalent to::
92
93 def accumulate(iterable, start=0):
94 'Return running totals'
95 # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
96 total = start
97 for element in iterable:
98 total += element
99 yield total
100
101 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103.. function:: chain(*iterables)
104
Raymond Hettinger30c73622010-12-01 23:45:20 +0000105 Make an iterator that returns elements from the first iterable until it is
106 exhausted, then proceeds to the next iterable, until all of the iterables are
107 exhausted. Used for treating consecutive sequences as a single sequence.
108 Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Raymond Hettinger30c73622010-12-01 23:45:20 +0000110 def chain(*iterables):
111 # chain('ABC', 'DEF') --> A B C D E F
112 for it in iterables:
113 for element in it:
114 yield element
Georg Brandl116aa622007-08-15 14:28:22 +0000115
116
Georg Brandl933b9742010-07-29 14:36:11 +0000117.. classmethod:: chain.from_iterable(iterable)
Christian Heimes70e7ea22008-02-28 20:02:27 +0000118
Raymond Hettinger30c73622010-12-01 23:45:20 +0000119 Alternate constructor for :func:`chain`. Gets chained inputs from a
120 single iterable argument that is evaluated lazily. Equivalent to::
Christian Heimes70e7ea22008-02-28 20:02:27 +0000121
Raymond Hettinger30c73622010-12-01 23:45:20 +0000122 @classmethod
123 def from_iterable(iterables):
124 # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
125 for it in iterables:
126 for element in it:
127 yield element
Christian Heimes70e7ea22008-02-28 20:02:27 +0000128
Christian Heimes78644762008-03-04 23:39:23 +0000129
Christian Heimes836baa52008-02-26 08:18:30 +0000130.. function:: combinations(iterable, r)
131
Raymond Hettinger30c73622010-12-01 23:45:20 +0000132 Return *r* length subsequences of elements from the input *iterable*.
Christian Heimes836baa52008-02-26 08:18:30 +0000133
Raymond Hettinger30c73622010-12-01 23:45:20 +0000134 Combinations are emitted in lexicographic sort order. So, if the
135 input *iterable* is sorted, the combination tuples will be produced
136 in sorted order.
Christian Heimes836baa52008-02-26 08:18:30 +0000137
Raymond Hettinger30c73622010-12-01 23:45:20 +0000138 Elements are treated as unique based on their position, not on their
139 value. So if the input elements are unique, there will be no repeat
140 values in each combination.
Christian Heimes836baa52008-02-26 08:18:30 +0000141
Raymond Hettinger30c73622010-12-01 23:45:20 +0000142 Equivalent to::
Christian Heimes836baa52008-02-26 08:18:30 +0000143
144 def combinations(iterable, r):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000145 # combinations('ABCD', 2) --> AB AC AD BC BD CD
146 # combinations(range(4), 3) --> 012 013 023 123
Christian Heimes836baa52008-02-26 08:18:30 +0000147 pool = tuple(iterable)
Christian Heimes380f7f22008-02-28 11:19:05 +0000148 n = len(pool)
Raymond Hettinger5bad41e2009-01-08 21:01:54 +0000149 if r > n:
150 return
151 indices = list(range(r))
Christian Heimesb558a2e2008-03-02 22:46:37 +0000152 yield tuple(pool[i] for i in indices)
Raymond Hettingercf984ce2009-02-18 20:56:51 +0000153 while True:
Christian Heimes380f7f22008-02-28 11:19:05 +0000154 for i in reversed(range(r)):
Christian Heimesb558a2e2008-03-02 22:46:37 +0000155 if indices[i] != i + n - r:
Christian Heimes836baa52008-02-26 08:18:30 +0000156 break
Christian Heimes380f7f22008-02-28 11:19:05 +0000157 else:
158 return
Christian Heimesb558a2e2008-03-02 22:46:37 +0000159 indices[i] += 1
Christian Heimes380f7f22008-02-28 11:19:05 +0000160 for j in range(i+1, r):
Christian Heimesb558a2e2008-03-02 22:46:37 +0000161 indices[j] = indices[j-1] + 1
162 yield tuple(pool[i] for i in indices)
Christian Heimes836baa52008-02-26 08:18:30 +0000163
Raymond Hettinger30c73622010-12-01 23:45:20 +0000164 The code for :func:`combinations` can be also expressed as a subsequence
165 of :func:`permutations` after filtering entries where the elements are not
166 in sorted order (according to their position in the input pool)::
Christian Heimes78644762008-03-04 23:39:23 +0000167
168 def combinations(iterable, r):
169 pool = tuple(iterable)
170 n = len(pool)
171 for indices in permutations(range(n), r):
172 if sorted(indices) == list(indices):
173 yield tuple(pool[i] for i in indices)
174
Raymond Hettinger30c73622010-12-01 23:45:20 +0000175 The number of items returned is ``n! / r! / (n-r)!`` when ``0 <= r <= n``
176 or zero when ``r > n``.
Christian Heimes836baa52008-02-26 08:18:30 +0000177
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000178.. function:: combinations_with_replacement(iterable, r)
179
Raymond Hettinger30c73622010-12-01 23:45:20 +0000180 Return *r* length subsequences of elements from the input *iterable*
181 allowing individual elements to be repeated more than once.
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000182
Raymond Hettinger30c73622010-12-01 23:45:20 +0000183 Combinations are emitted in lexicographic sort order. So, if the
184 input *iterable* is sorted, the combination tuples will be produced
185 in sorted order.
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000186
Raymond Hettinger30c73622010-12-01 23:45:20 +0000187 Elements are treated as unique based on their position, not on their
188 value. So if the input elements are unique, the generated combinations
189 will also be unique.
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000190
Raymond Hettinger30c73622010-12-01 23:45:20 +0000191 Equivalent to::
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000192
193 def combinations_with_replacement(iterable, r):
194 # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
195 pool = tuple(iterable)
196 n = len(pool)
197 if not n and r:
198 return
199 indices = [0] * r
200 yield tuple(pool[i] for i in indices)
Raymond Hettingercf984ce2009-02-18 20:56:51 +0000201 while True:
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000202 for i in reversed(range(r)):
203 if indices[i] != n - 1:
204 break
205 else:
206 return
207 indices[i:] = [indices[i] + 1] * (r - i)
208 yield tuple(pool[i] for i in indices)
209
Raymond Hettinger30c73622010-12-01 23:45:20 +0000210 The code for :func:`combinations_with_replacement` can be also expressed as
211 a subsequence of :func:`product` after filtering entries where the elements
212 are not in sorted order (according to their position in the input pool)::
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000213
214 def combinations_with_replacement(iterable, r):
215 pool = tuple(iterable)
216 n = len(pool)
217 for indices in product(range(n), repeat=r):
218 if sorted(indices) == list(indices):
219 yield tuple(pool[i] for i in indices)
220
Raymond Hettinger30c73622010-12-01 23:45:20 +0000221 The number of items returned is ``(n+r-1)! / r! / (n-1)!`` when ``n > 0``.
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000222
Raymond Hettinger30c73622010-12-01 23:45:20 +0000223 .. versionadded:: 3.1
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000224
Georg Brandl67b21b72010-08-17 15:07:14 +0000225
Raymond Hettinger6b3b0fc2009-01-26 02:56:58 +0000226.. function:: compress(data, selectors)
227
Raymond Hettinger30c73622010-12-01 23:45:20 +0000228 Make an iterator that filters elements from *data* returning only those that
229 have a corresponding element in *selectors* that evaluates to ``True``.
230 Stops when either the *data* or *selectors* iterables has been exhausted.
231 Equivalent to::
Raymond Hettinger6b3b0fc2009-01-26 02:56:58 +0000232
Raymond Hettinger30c73622010-12-01 23:45:20 +0000233 def compress(data, selectors):
234 # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
235 return (d for d, s in zip(data, selectors) if s)
Raymond Hettinger6b3b0fc2009-01-26 02:56:58 +0000236
Raymond Hettinger30c73622010-12-01 23:45:20 +0000237 .. versionadded:: 3.1
Raymond Hettinger6b3b0fc2009-01-26 02:56:58 +0000238
239
Raymond Hettinger9e8dbbc2009-02-14 04:21:49 +0000240.. function:: count(start=0, step=1)
Georg Brandl116aa622007-08-15 14:28:22 +0000241
Raymond Hettinger30c73622010-12-01 23:45:20 +0000242 Make an iterator that returns evenly spaced values starting with *n*. Often
243 used as an argument to :func:`map` to generate consecutive data points.
244 Also, used with :func:`zip` to add sequence numbers. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000245
Raymond Hettinger30c73622010-12-01 23:45:20 +0000246 def count(start=0, step=1):
247 # count(10) --> 10 11 12 13 14 ...
248 # count(2.5, 0.5) -> 3.5 3.0 4.5 ...
249 n = start
250 while True:
251 yield n
252 n += step
Georg Brandl116aa622007-08-15 14:28:22 +0000253
Raymond Hettinger30c73622010-12-01 23:45:20 +0000254 When counting with floating point numbers, better accuracy can sometimes be
255 achieved by substituting multiplicative code such as: ``(start + step * i
256 for i in count())``.
Raymond Hettinger5bc472a2009-06-17 01:40:52 +0000257
Raymond Hettinger30c73622010-12-01 23:45:20 +0000258 .. versionchanged:: 3.1
259 Added *step* argument and allowed non-integer arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000260
261.. function:: cycle(iterable)
262
Raymond Hettinger30c73622010-12-01 23:45:20 +0000263 Make an iterator returning elements from the iterable and saving a copy of each.
264 When the iterable is exhausted, return elements from the saved copy. Repeats
265 indefinitely. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000266
Raymond Hettinger30c73622010-12-01 23:45:20 +0000267 def cycle(iterable):
268 # cycle('ABCD') --> A B C D A B C D A B C D ...
269 saved = []
270 for element in iterable:
271 yield element
272 saved.append(element)
273 while saved:
274 for element in saved:
Georg Brandl116aa622007-08-15 14:28:22 +0000275 yield element
276
Raymond Hettinger30c73622010-12-01 23:45:20 +0000277 Note, this member of the toolkit may require significant auxiliary storage
278 (depending on the length of the iterable).
Georg Brandl116aa622007-08-15 14:28:22 +0000279
280
281.. function:: dropwhile(predicate, iterable)
282
Raymond Hettinger30c73622010-12-01 23:45:20 +0000283 Make an iterator that drops elements from the iterable as long as the predicate
284 is true; afterwards, returns every element. Note, the iterator does not produce
285 *any* output until the predicate first becomes false, so it may have a lengthy
286 start-up time. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000287
Raymond Hettinger30c73622010-12-01 23:45:20 +0000288 def dropwhile(predicate, iterable):
289 # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
290 iterable = iter(iterable)
291 for x in iterable:
292 if not predicate(x):
293 yield x
294 break
295 for x in iterable:
296 yield x
Georg Brandl116aa622007-08-15 14:28:22 +0000297
Raymond Hettinger749761e2009-01-27 04:42:48 +0000298.. function:: filterfalse(predicate, iterable)
299
Raymond Hettinger30c73622010-12-01 23:45:20 +0000300 Make an iterator that filters elements from iterable returning only those for
301 which the predicate is ``False``. If *predicate* is ``None``, return the items
302 that are false. Equivalent to::
Raymond Hettinger749761e2009-01-27 04:42:48 +0000303
Raymond Hettinger30c73622010-12-01 23:45:20 +0000304 def filterfalse(predicate, iterable):
305 # filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
306 if predicate is None:
307 predicate = bool
308 for x in iterable:
309 if not predicate(x):
310 yield x
Raymond Hettinger749761e2009-01-27 04:42:48 +0000311
Georg Brandl116aa622007-08-15 14:28:22 +0000312
Georg Brandl3dd33882009-06-01 17:35:27 +0000313.. function:: groupby(iterable, key=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000314
Raymond Hettinger30c73622010-12-01 23:45:20 +0000315 Make an iterator that returns consecutive keys and groups from the *iterable*.
316 The *key* is a function computing a key value for each element. If not
317 specified or is ``None``, *key* defaults to an identity function and returns
318 the element unchanged. Generally, the iterable needs to already be sorted on
319 the same key function.
Georg Brandl116aa622007-08-15 14:28:22 +0000320
Raymond Hettinger30c73622010-12-01 23:45:20 +0000321 The operation of :func:`groupby` is similar to the ``uniq`` filter in Unix. It
322 generates a break or new group every time the value of the key function changes
323 (which is why it is usually necessary to have sorted the data using the same key
324 function). That behavior differs from SQL's GROUP BY which aggregates common
325 elements regardless of their input order.
Georg Brandl116aa622007-08-15 14:28:22 +0000326
Raymond Hettinger30c73622010-12-01 23:45:20 +0000327 The returned group is itself an iterator that shares the underlying iterable
328 with :func:`groupby`. Because the source is shared, when the :func:`groupby`
329 object is advanced, the previous group is no longer visible. So, if that data
330 is needed later, it should be stored as a list::
Georg Brandl116aa622007-08-15 14:28:22 +0000331
Raymond Hettinger30c73622010-12-01 23:45:20 +0000332 groups = []
333 uniquekeys = []
334 data = sorted(data, key=keyfunc)
335 for k, g in groupby(data, keyfunc):
336 groups.append(list(g)) # Store group iterator as a list
337 uniquekeys.append(k)
Georg Brandl116aa622007-08-15 14:28:22 +0000338
Raymond Hettinger30c73622010-12-01 23:45:20 +0000339 :func:`groupby` is equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000340
Raymond Hettinger30c73622010-12-01 23:45:20 +0000341 class groupby:
342 # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
343 # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
344 def __init__(self, iterable, key=None):
345 if key is None:
346 key = lambda x: x
347 self.keyfunc = key
348 self.it = iter(iterable)
349 self.tgtkey = self.currkey = self.currvalue = object()
350 def __iter__(self):
351 return self
352 def __next__(self):
353 while self.currkey == self.tgtkey:
354 self.currvalue = next(self.it) # Exit on StopIteration
355 self.currkey = self.keyfunc(self.currvalue)
356 self.tgtkey = self.currkey
357 return (self.currkey, self._grouper(self.tgtkey))
358 def _grouper(self, tgtkey):
359 while self.currkey == tgtkey:
360 yield self.currvalue
361 self.currvalue = next(self.it) # Exit on StopIteration
362 self.currkey = self.keyfunc(self.currvalue)
Georg Brandl116aa622007-08-15 14:28:22 +0000363
Georg Brandl116aa622007-08-15 14:28:22 +0000364
Georg Brandl116aa622007-08-15 14:28:22 +0000365.. function:: islice(iterable, [start,] stop [, step])
366
Raymond Hettinger30c73622010-12-01 23:45:20 +0000367 Make an iterator that returns selected elements from the iterable. If *start* is
368 non-zero, then elements from the iterable are skipped until start is reached.
369 Afterward, elements are returned consecutively unless *step* is set higher than
370 one which results in items being skipped. If *stop* is ``None``, then iteration
371 continues until the iterator is exhausted, if at all; otherwise, it stops at the
372 specified position. Unlike regular slicing, :func:`islice` does not support
373 negative values for *start*, *stop*, or *step*. Can be used to extract related
374 fields from data where the internal structure has been flattened (for example, a
375 multi-line report may list a name field on every third line). Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000376
Raymond Hettinger30c73622010-12-01 23:45:20 +0000377 def islice(iterable, *args):
378 # islice('ABCDEFG', 2) --> A B
379 # islice('ABCDEFG', 2, 4) --> C D
380 # islice('ABCDEFG', 2, None) --> C D E F G
381 # islice('ABCDEFG', 0, None, 2) --> A C E G
382 s = slice(*args)
383 it = iter(range(s.start or 0, s.stop or sys.maxsize, s.step or 1))
384 nexti = next(it)
385 for i, element in enumerate(iterable):
386 if i == nexti:
387 yield element
388 nexti = next(it)
Georg Brandl116aa622007-08-15 14:28:22 +0000389
Raymond Hettinger30c73622010-12-01 23:45:20 +0000390 If *start* is ``None``, then iteration starts at zero. If *step* is ``None``,
391 then the step defaults to one.
Georg Brandl116aa622007-08-15 14:28:22 +0000392
Georg Brandl116aa622007-08-15 14:28:22 +0000393
Georg Brandl3dd33882009-06-01 17:35:27 +0000394.. function:: permutations(iterable, r=None)
Christian Heimes70e7ea22008-02-28 20:02:27 +0000395
Raymond Hettinger30c73622010-12-01 23:45:20 +0000396 Return successive *r* length permutations of elements in the *iterable*.
Christian Heimes70e7ea22008-02-28 20:02:27 +0000397
Raymond Hettinger30c73622010-12-01 23:45:20 +0000398 If *r* is not specified or is ``None``, then *r* defaults to the length
399 of the *iterable* and all possible full-length permutations
400 are generated.
Christian Heimes70e7ea22008-02-28 20:02:27 +0000401
Raymond Hettinger30c73622010-12-01 23:45:20 +0000402 Permutations are emitted in lexicographic sort order. So, if the
403 input *iterable* is sorted, the permutation tuples will be produced
404 in sorted order.
Christian Heimes70e7ea22008-02-28 20:02:27 +0000405
Raymond Hettinger30c73622010-12-01 23:45:20 +0000406 Elements are treated as unique based on their position, not on their
407 value. So if the input elements are unique, there will be no repeat
408 values in each permutation.
Christian Heimes70e7ea22008-02-28 20:02:27 +0000409
Raymond Hettinger30c73622010-12-01 23:45:20 +0000410 Equivalent to::
Christian Heimesb558a2e2008-03-02 22:46:37 +0000411
412 def permutations(iterable, r=None):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000413 # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
414 # permutations(range(3)) --> 012 021 102 120 201 210
Christian Heimesb558a2e2008-03-02 22:46:37 +0000415 pool = tuple(iterable)
416 n = len(pool)
417 r = n if r is None else r
Raymond Hettinger5bad41e2009-01-08 21:01:54 +0000418 if r > n:
419 return
420 indices = list(range(n))
Christian Heimesfe337bf2008-03-23 21:54:12 +0000421 cycles = range(n, n-r, -1)
Christian Heimesb558a2e2008-03-02 22:46:37 +0000422 yield tuple(pool[i] for i in indices[:r])
423 while n:
424 for i in reversed(range(r)):
425 cycles[i] -= 1
426 if cycles[i] == 0:
427 indices[i:] = indices[i+1:] + indices[i:i+1]
428 cycles[i] = n - i
429 else:
430 j = cycles[i]
431 indices[i], indices[-j] = indices[-j], indices[i]
432 yield tuple(pool[i] for i in indices[:r])
433 break
434 else:
435 return
Christian Heimes70e7ea22008-02-28 20:02:27 +0000436
Raymond Hettinger30c73622010-12-01 23:45:20 +0000437 The code for :func:`permutations` can be also expressed as a subsequence of
438 :func:`product`, filtered to exclude entries with repeated elements (those
439 from the same position in the input pool)::
Christian Heimes78644762008-03-04 23:39:23 +0000440
441 def permutations(iterable, r=None):
442 pool = tuple(iterable)
443 n = len(pool)
444 r = n if r is None else r
445 for indices in product(range(n), repeat=r):
446 if len(set(indices)) == r:
447 yield tuple(pool[i] for i in indices)
448
Raymond Hettinger30c73622010-12-01 23:45:20 +0000449 The number of items returned is ``n! / (n-r)!`` when ``0 <= r <= n``
450 or zero when ``r > n``.
Christian Heimes70e7ea22008-02-28 20:02:27 +0000451
Georg Brandl3dd33882009-06-01 17:35:27 +0000452.. function:: product(*iterables, repeat=1)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000453
Raymond Hettinger30c73622010-12-01 23:45:20 +0000454 Cartesian product of input iterables.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000455
Raymond Hettinger30c73622010-12-01 23:45:20 +0000456 Equivalent to nested for-loops in a generator expression. For example,
457 ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000458
Raymond Hettinger30c73622010-12-01 23:45:20 +0000459 The nested loops cycle like an odometer with the rightmost element advancing
460 on every iteration. This pattern creates a lexicographic ordering so that if
461 the input's iterables are sorted, the product tuples are emitted in sorted
462 order.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000463
Raymond Hettinger30c73622010-12-01 23:45:20 +0000464 To compute the product of an iterable with itself, specify the number of
465 repetitions with the optional *repeat* keyword argument. For example,
466 ``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``.
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000467
Raymond Hettinger30c73622010-12-01 23:45:20 +0000468 This function is equivalent to the following code, except that the
469 actual implementation does not build up intermediate results in memory::
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000470
Raymond Hettinger30c73622010-12-01 23:45:20 +0000471 def product(*args, repeat=1):
472 # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
473 # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
474 pools = [tuple(pool) for pool in args] * repeat
475 result = [[]]
476 for pool in pools:
477 result = [x+[y] for x in result for y in pool]
478 for prod in result:
479 yield tuple(prod)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000480
481
Raymond Hettingerd75ad442009-06-01 19:16:52 +0000482.. function:: repeat(object[, times])
Georg Brandl116aa622007-08-15 14:28:22 +0000483
Raymond Hettinger30c73622010-12-01 23:45:20 +0000484 Make an iterator that returns *object* over and over again. Runs indefinitely
485 unless the *times* argument is specified. Used as argument to :func:`map` for
486 invariant parameters to the called function. Also used with :func:`zip` to
487 create an invariant part of a tuple record. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000488
Raymond Hettinger30c73622010-12-01 23:45:20 +0000489 def repeat(object, times=None):
490 # repeat(10, 3) --> 10 10 10
491 if times is None:
492 while True:
493 yield object
494 else:
495 for i in range(times):
496 yield object
Georg Brandl116aa622007-08-15 14:28:22 +0000497
498
499.. function:: starmap(function, iterable)
500
Raymond Hettinger30c73622010-12-01 23:45:20 +0000501 Make an iterator that computes the function using arguments obtained from
502 the iterable. Used instead of :func:`map` when argument parameters are already
503 grouped in tuples from a single iterable (the data has been "pre-zipped"). The
504 difference between :func:`map` and :func:`starmap` parallels the distinction
505 between ``function(a,b)`` and ``function(*c)``. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000506
Raymond Hettinger30c73622010-12-01 23:45:20 +0000507 def starmap(function, iterable):
508 # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
509 for args in iterable:
510 yield function(*args)
Christian Heimes679db4a2008-01-18 09:56:22 +0000511
Georg Brandl116aa622007-08-15 14:28:22 +0000512
513.. function:: takewhile(predicate, iterable)
514
Raymond Hettinger30c73622010-12-01 23:45:20 +0000515 Make an iterator that returns elements from the iterable as long as the
516 predicate is true. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000517
Raymond Hettinger30c73622010-12-01 23:45:20 +0000518 def takewhile(predicate, iterable):
519 # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
520 for x in iterable:
521 if predicate(x):
522 yield x
523 else:
524 break
Georg Brandl116aa622007-08-15 14:28:22 +0000525
526
Georg Brandl3dd33882009-06-01 17:35:27 +0000527.. function:: tee(iterable, n=2)
Georg Brandl116aa622007-08-15 14:28:22 +0000528
Raymond Hettinger30c73622010-12-01 23:45:20 +0000529 Return *n* independent iterators from a single iterable. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000530
Raymond Hettingercf984ce2009-02-18 20:56:51 +0000531 def tee(iterable, n=2):
532 it = iter(iterable)
533 deques = [collections.deque() for i in range(n)]
534 def gen(mydeque):
535 while True:
536 if not mydeque: # when the local deque is empty
537 newval = next(it) # fetch a new value and
538 for d in deques: # load it to all the deques
539 d.append(newval)
540 yield mydeque.popleft()
541 return tuple(gen(d) for d in deques)
Georg Brandl116aa622007-08-15 14:28:22 +0000542
Raymond Hettinger30c73622010-12-01 23:45:20 +0000543 Once :func:`tee` has made a split, the original *iterable* should not be
544 used anywhere else; otherwise, the *iterable* could get advanced without
545 the tee objects being informed.
Georg Brandl116aa622007-08-15 14:28:22 +0000546
Raymond Hettinger30c73622010-12-01 23:45:20 +0000547 This itertool may require significant auxiliary storage (depending on how
548 much temporary data needs to be stored). In general, if one iterator uses
549 most or all of the data before another iterator starts, it is faster to use
550 :func:`list` instead of :func:`tee`.
Georg Brandl116aa622007-08-15 14:28:22 +0000551
Georg Brandl116aa622007-08-15 14:28:22 +0000552
Georg Brandl3dd33882009-06-01 17:35:27 +0000553.. function:: zip_longest(*iterables, fillvalue=None)
Raymond Hettinger749761e2009-01-27 04:42:48 +0000554
Raymond Hettinger30c73622010-12-01 23:45:20 +0000555 Make an iterator that aggregates elements from each of the iterables. If the
556 iterables are of uneven length, missing values are filled-in with *fillvalue*.
557 Iteration continues until the longest iterable is exhausted. Equivalent to::
Raymond Hettinger749761e2009-01-27 04:42:48 +0000558
Raymond Hettinger30c73622010-12-01 23:45:20 +0000559 def zip_longest(*args, fillvalue=None):
560 # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
561 def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
562 yield counter() # yields the fillvalue, or raises IndexError
563 fillers = repeat(fillvalue)
564 iters = [chain(it, sentinel(), fillers) for it in args]
565 try:
566 for tup in zip(*iters):
567 yield tup
568 except IndexError:
569 pass
Raymond Hettinger749761e2009-01-27 04:42:48 +0000570
Raymond Hettinger30c73622010-12-01 23:45:20 +0000571 If one of the iterables is potentially infinite, then the :func:`zip_longest`
572 function should be wrapped with something that limits the number of calls
573 (for example :func:`islice` or :func:`takewhile`). If not specified,
574 *fillvalue* defaults to ``None``.
Raymond Hettinger749761e2009-01-27 04:42:48 +0000575
576
Georg Brandl116aa622007-08-15 14:28:22 +0000577.. _itertools-recipes:
578
579Recipes
580-------
581
582This section shows recipes for creating an extended toolset using the existing
583itertools as building blocks.
584
585The extended tools offer the same high performance as the underlying toolset.
586The superior memory performance is kept by processing elements one at a time
587rather than bringing the whole iterable into memory all at once. Code volume is
588kept small by linking the tools together in a functional style which helps
589eliminate temporary variables. High speed is retained by preferring
Georg Brandl9afde1c2007-11-01 20:32:30 +0000590"vectorized" building blocks over the use of for-loops and :term:`generator`\s
Christian Heimesfe337bf2008-03-23 21:54:12 +0000591which incur interpreter overhead.
592
593.. testcode::
Georg Brandl116aa622007-08-15 14:28:22 +0000594
Raymond Hettinger30c73622010-12-01 23:45:20 +0000595 def take(n, iterable):
596 "Return first n items of the iterable as a list"
597 return list(islice(iterable, n))
Georg Brandl116aa622007-08-15 14:28:22 +0000598
Raymond Hettinger30c73622010-12-01 23:45:20 +0000599 def tabulate(function, start=0):
600 "Return function(0), function(1), ..."
601 return map(function, count(start))
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000602
Raymond Hettinger30c73622010-12-01 23:45:20 +0000603 def consume(iterator, n):
604 "Advance the iterator n-steps ahead. If n is none, consume entirely."
605 # Use functions that consume iterators at C speed.
606 if n is None:
607 # feed the entire iterator into a zero-length deque
608 collections.deque(iterator, maxlen=0)
609 else:
610 # advance to the empty slice starting at position n
611 next(islice(iterator, n, n), None)
Raymond Hettingerfa007962009-03-09 11:55:25 +0000612
Raymond Hettinger30c73622010-12-01 23:45:20 +0000613 def nth(iterable, n, default=None):
614 "Returns the nth item or a default value"
615 return next(islice(iterable, n, None), default)
Georg Brandl116aa622007-08-15 14:28:22 +0000616
Raymond Hettinger30c73622010-12-01 23:45:20 +0000617 def quantify(iterable, pred=bool):
618 "Count how many times the predicate is true"
619 return sum(map(pred, iterable))
Georg Brandl116aa622007-08-15 14:28:22 +0000620
Raymond Hettinger30c73622010-12-01 23:45:20 +0000621 def padnone(iterable):
622 """Returns the sequence elements and then returns None indefinitely.
Georg Brandl116aa622007-08-15 14:28:22 +0000623
Raymond Hettinger30c73622010-12-01 23:45:20 +0000624 Useful for emulating the behavior of the built-in map() function.
625 """
626 return chain(iterable, repeat(None))
Georg Brandl116aa622007-08-15 14:28:22 +0000627
Raymond Hettinger30c73622010-12-01 23:45:20 +0000628 def ncycles(iterable, n):
629 "Returns the sequence elements n times"
630 return chain.from_iterable(repeat(tuple(iterable), n))
Georg Brandl116aa622007-08-15 14:28:22 +0000631
Raymond Hettinger30c73622010-12-01 23:45:20 +0000632 def dotproduct(vec1, vec2):
633 return sum(map(operator.mul, vec1, vec2))
Georg Brandl116aa622007-08-15 14:28:22 +0000634
Raymond Hettinger30c73622010-12-01 23:45:20 +0000635 def flatten(listOfLists):
636 "Flatten one level of nesting"
637 return chain.from_iterable(listOfLists)
Georg Brandl116aa622007-08-15 14:28:22 +0000638
Raymond Hettinger30c73622010-12-01 23:45:20 +0000639 def repeatfunc(func, times=None, *args):
640 """Repeat calls to func with specified arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000641
Raymond Hettinger30c73622010-12-01 23:45:20 +0000642 Example: repeatfunc(random.random)
643 """
644 if times is None:
645 return starmap(func, repeat(args))
646 return starmap(func, repeat(args, times))
Georg Brandl116aa622007-08-15 14:28:22 +0000647
Raymond Hettinger30c73622010-12-01 23:45:20 +0000648 def pairwise(iterable):
649 "s -> (s0,s1), (s1,s2), (s2, s3), ..."
650 a, b = tee(iterable)
651 next(b, None)
652 return zip(a, b)
Georg Brandl116aa622007-08-15 14:28:22 +0000653
Raymond Hettinger30c73622010-12-01 23:45:20 +0000654 def grouper(n, iterable, fillvalue=None):
655 "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
656 args = [iter(iterable)] * n
657 return zip_longest(*args, fillvalue=fillvalue)
Georg Brandl116aa622007-08-15 14:28:22 +0000658
Raymond Hettinger30c73622010-12-01 23:45:20 +0000659 def roundrobin(*iterables):
660 "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
661 # Recipe credited to George Sakkis
662 pending = len(iterables)
663 nexts = cycle(iter(it).__next__ for it in iterables)
664 while pending:
665 try:
666 for next in nexts:
667 yield next()
668 except StopIteration:
669 pending -= 1
670 nexts = cycle(islice(nexts, pending))
Georg Brandl116aa622007-08-15 14:28:22 +0000671
Raymond Hettinger30c73622010-12-01 23:45:20 +0000672 def partition(pred, iterable):
673 'Use a predicate to partition entries into false entries and true entries'
674 # partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
675 t1, t2 = tee(iterable)
676 return filterfalse(pred, t1), filter(pred, t2)
Raymond Hettinger5ce0aa22010-12-01 10:49:19 +0000677
Raymond Hettinger30c73622010-12-01 23:45:20 +0000678 def powerset(iterable):
679 "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
680 s = list(iterable)
681 return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
Raymond Hettinger08d01ee2010-08-07 05:36:53 +0000682
Raymond Hettinger30c73622010-12-01 23:45:20 +0000683 def unique_everseen(iterable, key=None):
684 "List unique elements, preserving order. Remember all elements ever seen."
685 # unique_everseen('AAAABBBCCDAABBB') --> A B C D
686 # unique_everseen('ABBCcAD', str.lower) --> A B C D
687 seen = set()
688 seen_add = seen.add
689 if key is None:
690 for element in filterfalse(seen.__contains__, iterable):
691 seen_add(element)
692 yield element
693 else:
694 for element in iterable:
695 k = key(element)
696 if k not in seen:
697 seen_add(k)
698 yield element
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000699
Raymond Hettinger30c73622010-12-01 23:45:20 +0000700 def unique_justseen(iterable, key=None):
701 "List unique elements, preserving order. Remember only the element just seen."
702 # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
703 # unique_justseen('ABBCcAD', str.lower) --> A B C A D
704 return map(next, map(itemgetter(1), groupby(iterable, key)))
Raymond Hettingerad9d96b2009-01-02 21:39:07 +0000705
Raymond Hettinger30c73622010-12-01 23:45:20 +0000706 def iter_except(func, exception, first=None):
707 """ Call a function repeatedly until an exception is raised.
Raymond Hettingerfc91aa22010-03-28 18:27:13 +0000708
Raymond Hettinger30c73622010-12-01 23:45:20 +0000709 Converts a call-until-exception interface to an iterator interface.
710 Like __builtin__.iter(func, sentinel) but uses an exception instead
711 of a sentinel to end the loop.
Raymond Hettingerfc91aa22010-03-28 18:27:13 +0000712
Raymond Hettinger30c73622010-12-01 23:45:20 +0000713 Examples:
714 iter_except(functools.partial(heappop, h), IndexError) # priority queue iterator
715 iter_except(d.popitem, KeyError) # non-blocking dict iterator
716 iter_except(d.popleft, IndexError) # non-blocking deque iterator
717 iter_except(q.get_nowait, Queue.Empty) # loop over a producer Queue
718 iter_except(s.pop, KeyError) # non-blocking set iterator
Raymond Hettingerfc91aa22010-03-28 18:27:13 +0000719
Raymond Hettinger30c73622010-12-01 23:45:20 +0000720 """
721 try:
722 if first is not None:
723 yield first() # For database APIs needing an initial cast to db.first()
724 while 1:
725 yield func()
726 except exception:
727 pass
Raymond Hettingerfc91aa22010-03-28 18:27:13 +0000728
Raymond Hettinger30c73622010-12-01 23:45:20 +0000729 def random_product(*args, repeat=1):
730 "Random selection from itertools.product(*args, **kwds)"
731 pools = [tuple(pool) for pool in args] * repeat
732 return tuple(random.choice(pool) for pool in pools)
Raymond Hettingerfc91aa22010-03-28 18:27:13 +0000733
Raymond Hettinger30c73622010-12-01 23:45:20 +0000734 def random_permutation(iterable, r=None):
735 "Random selection from itertools.permutations(iterable, r)"
736 pool = tuple(iterable)
737 r = len(pool) if r is None else r
738 return tuple(random.sample(pool, r))
Raymond Hettinger063a4b62010-04-02 04:18:18 +0000739
Raymond Hettinger30c73622010-12-01 23:45:20 +0000740 def random_combination(iterable, r):
741 "Random selection from itertools.combinations(iterable, r)"
742 pool = tuple(iterable)
743 n = len(pool)
744 indices = sorted(random.sample(range(n), r))
745 return tuple(pool[i] for i in indices)
Raymond Hettinger063a4b62010-04-02 04:18:18 +0000746
Raymond Hettinger30c73622010-12-01 23:45:20 +0000747 def random_combination_with_replacement(iterable, r):
748 "Random selection from itertools.combinations_with_replacement(iterable, r)"
749 pool = tuple(iterable)
750 n = len(pool)
751 indices = sorted(random.randrange(n) for i in range(r))
752 return tuple(pool[i] for i in indices)
Raymond Hettinger063a4b62010-04-02 04:18:18 +0000753
Raymond Hettingerfc91aa22010-03-28 18:27:13 +0000754Note, many of the above recipes can be optimized by replacing global lookups
755with local variables defined as default values. For example, the
756*dotproduct* recipe can be written as::
757
Raymond Hettinger30c73622010-12-01 23:45:20 +0000758 def dotproduct(vec1, vec2, sum=sum, map=map, mul=operator.mul):
759 return sum(map(mul, vec1, vec2))