blob: 9b64c7d2d6e37b0927a29722a6e8f56e43f1be3d [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 Hettinger5d446132011-03-27 18:52:10 -070049:func:`accumulate` p [,func] 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 Hettinger5d446132011-03-27 18:52:10 -070087.. function:: accumulate(iterable[, func])
Raymond Hettingeradb81462010-12-01 22:50:36 +000088
Raymond Hettinger2d93e6e2010-12-03 02:33:53 +000089 Make an iterator that returns accumulated sums. Elements may be any addable
Raymond Hettinger5d446132011-03-27 18:52:10 -070090 type including :class:`Decimal` or :class:`Fraction`. If the optional
91 *func* argument is supplied, it should be a function of two arguments
92 and it will be used instead of addition.
Raymond Hettingeradb81462010-12-01 22:50:36 +000093
Raymond Hettinger5d446132011-03-27 18:52:10 -070094 Equivalent to::
95
96 def accumulate(iterable, func=operator.add):
Raymond Hettingeradb81462010-12-01 22:50:36 +000097 'Return running totals'
Raymond Hettingerd8ff4652010-12-03 02:09:34 +000098 # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
Raymond Hettinger5d446132011-03-27 18:52:10 -070099 # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
Raymond Hettingerd8ff4652010-12-03 02:09:34 +0000100 it = iter(iterable)
101 total = next(it)
102 yield total
103 for element in it:
Raymond Hettinger5d446132011-03-27 18:52:10 -0700104 total = func(total, element)
Raymond Hettingerd8ff4652010-12-03 02:09:34 +0000105 yield total
Raymond Hettingeradb81462010-12-01 22:50:36 +0000106
Raymond Hettinger295c1d42011-04-21 11:09:28 -0700107 There are a number of uses for the *func* argument. It can be set to
108 :func:`min` for a running minimum, :func:`max` for a running maximum, or
109 :func:`operator.mul` for a running product. Amortization tables can be
110 built by accumulating interest and applying payments. First-order
111 `recurrence relations <http://en.wikipedia.org/wiki/Recurrence_relation>`_
112 can be modeled by supplying the initial value in the iterable and using only
113 the accumulated total in *func* argument::
Raymond Hettinger5d446132011-03-27 18:52:10 -0700114
115 >>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
116 >>> list(accumulate(data, operator.mul)) # running product
117 [3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
118 >>> list(accumulate(data, max)) # running maximum
119 [3, 4, 6, 6, 6, 9, 9, 9, 9, 9]
120
121 # Amortize a 5% loan of 1000 with 4 annual payments of 90
122 >>> cashflows = [1000, -90, -90, -90, -90]
123 >>> list(accumulate(cashflows, lambda bal, pmt: bal*1.05 + pmt))
124 [1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
125
Raymond Hettinger295c1d42011-04-21 11:09:28 -0700126 # Chaotic recurrence relation http://en.wikipedia.org/wiki/Logistic_map
127 >>> logistic_map = lambda x, _: r * x * (1 - x)
128 >>> r = 3.8
129 >>> x0 = 0.4
130 >>> inputs = repeat(x0, 36) # only the initial value is used
131 >>> [format(x, '.2f') for x in accumulate(inputs, logistic_map)]
132 ['0.40', '0.91', '0.30', '0.81', '0.60', '0.92', '0.29', '0.79', '0.63',
133 '0.88' ,'0.39', '0.90', '0.33', '0.84', '0.52', '0.95', '0.18', '0.57',
134 '0.93', '0.25', '0.71', '0.79', '0.63', '0.88', '0.39', '0.91', '0.32',
135 '0.83', '0.54', '0.95', '0.20', '0.60', '0.91', '0.30', '0.80', '0.60']
136
Raymond Hettingeradb81462010-12-01 22:50:36 +0000137 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000138
Raymond Hettinger5d446132011-03-27 18:52:10 -0700139 .. versionchanged:: 3.3
140 Added the optional *func* parameter.
141
Georg Brandl116aa622007-08-15 14:28:22 +0000142.. function:: chain(*iterables)
143
Raymond Hettinger30c73622010-12-01 23:45:20 +0000144 Make an iterator that returns elements from the first iterable until it is
145 exhausted, then proceeds to the next iterable, until all of the iterables are
146 exhausted. Used for treating consecutive sequences as a single sequence.
147 Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000148
Raymond Hettinger30c73622010-12-01 23:45:20 +0000149 def chain(*iterables):
150 # chain('ABC', 'DEF') --> A B C D E F
151 for it in iterables:
152 for element in it:
153 yield element
Georg Brandl116aa622007-08-15 14:28:22 +0000154
155
Georg Brandl933b9742010-07-29 14:36:11 +0000156.. classmethod:: chain.from_iterable(iterable)
Christian Heimes70e7ea22008-02-28 20:02:27 +0000157
Raymond Hettinger30c73622010-12-01 23:45:20 +0000158 Alternate constructor for :func:`chain`. Gets chained inputs from a
Raymond Hettinger1e21ebc2013-09-09 01:54:27 -0500159 single iterable argument that is evaluated lazily. Roughly equivalent to::
Christian Heimes70e7ea22008-02-28 20:02:27 +0000160
Raymond Hettinger30c73622010-12-01 23:45:20 +0000161 def from_iterable(iterables):
162 # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
163 for it in iterables:
164 for element in it:
165 yield element
Christian Heimes70e7ea22008-02-28 20:02:27 +0000166
Christian Heimes78644762008-03-04 23:39:23 +0000167
Christian Heimes836baa52008-02-26 08:18:30 +0000168.. function:: combinations(iterable, r)
169
Raymond Hettinger30c73622010-12-01 23:45:20 +0000170 Return *r* length subsequences of elements from the input *iterable*.
Christian Heimes836baa52008-02-26 08:18:30 +0000171
Raymond Hettinger30c73622010-12-01 23:45:20 +0000172 Combinations are emitted in lexicographic sort order. So, if the
173 input *iterable* is sorted, the combination tuples will be produced
174 in sorted order.
Christian Heimes836baa52008-02-26 08:18:30 +0000175
Raymond Hettinger30c73622010-12-01 23:45:20 +0000176 Elements are treated as unique based on their position, not on their
177 value. So if the input elements are unique, there will be no repeat
178 values in each combination.
Christian Heimes836baa52008-02-26 08:18:30 +0000179
Raymond Hettinger30c73622010-12-01 23:45:20 +0000180 Equivalent to::
Christian Heimes836baa52008-02-26 08:18:30 +0000181
182 def combinations(iterable, r):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000183 # combinations('ABCD', 2) --> AB AC AD BC BD CD
184 # combinations(range(4), 3) --> 012 013 023 123
Christian Heimes836baa52008-02-26 08:18:30 +0000185 pool = tuple(iterable)
Christian Heimes380f7f22008-02-28 11:19:05 +0000186 n = len(pool)
Raymond Hettinger5bad41e2009-01-08 21:01:54 +0000187 if r > n:
188 return
189 indices = list(range(r))
Christian Heimesb558a2e2008-03-02 22:46:37 +0000190 yield tuple(pool[i] for i in indices)
Raymond Hettingercf984ce2009-02-18 20:56:51 +0000191 while True:
Christian Heimes380f7f22008-02-28 11:19:05 +0000192 for i in reversed(range(r)):
Christian Heimesb558a2e2008-03-02 22:46:37 +0000193 if indices[i] != i + n - r:
Christian Heimes836baa52008-02-26 08:18:30 +0000194 break
Christian Heimes380f7f22008-02-28 11:19:05 +0000195 else:
196 return
Christian Heimesb558a2e2008-03-02 22:46:37 +0000197 indices[i] += 1
Christian Heimes380f7f22008-02-28 11:19:05 +0000198 for j in range(i+1, r):
Christian Heimesb558a2e2008-03-02 22:46:37 +0000199 indices[j] = indices[j-1] + 1
200 yield tuple(pool[i] for i in indices)
Christian Heimes836baa52008-02-26 08:18:30 +0000201
Raymond Hettinger30c73622010-12-01 23:45:20 +0000202 The code for :func:`combinations` can be also expressed as a subsequence
203 of :func:`permutations` after filtering entries where the elements are not
204 in sorted order (according to their position in the input pool)::
Christian Heimes78644762008-03-04 23:39:23 +0000205
206 def combinations(iterable, r):
207 pool = tuple(iterable)
208 n = len(pool)
209 for indices in permutations(range(n), r):
210 if sorted(indices) == list(indices):
211 yield tuple(pool[i] for i in indices)
212
Raymond Hettinger30c73622010-12-01 23:45:20 +0000213 The number of items returned is ``n! / r! / (n-r)!`` when ``0 <= r <= n``
214 or zero when ``r > n``.
Christian Heimes836baa52008-02-26 08:18:30 +0000215
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000216.. function:: combinations_with_replacement(iterable, r)
217
Raymond Hettinger30c73622010-12-01 23:45:20 +0000218 Return *r* length subsequences of elements from the input *iterable*
219 allowing individual elements to be repeated more than once.
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000220
Raymond Hettinger30c73622010-12-01 23:45:20 +0000221 Combinations are emitted in lexicographic sort order. So, if the
222 input *iterable* is sorted, the combination tuples will be produced
223 in sorted order.
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000224
Raymond Hettinger30c73622010-12-01 23:45:20 +0000225 Elements are treated as unique based on their position, not on their
226 value. So if the input elements are unique, the generated combinations
227 will also be unique.
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000228
Raymond Hettinger30c73622010-12-01 23:45:20 +0000229 Equivalent to::
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000230
231 def combinations_with_replacement(iterable, r):
232 # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
233 pool = tuple(iterable)
234 n = len(pool)
235 if not n and r:
236 return
237 indices = [0] * r
238 yield tuple(pool[i] for i in indices)
Raymond Hettingercf984ce2009-02-18 20:56:51 +0000239 while True:
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000240 for i in reversed(range(r)):
241 if indices[i] != n - 1:
242 break
243 else:
244 return
245 indices[i:] = [indices[i] + 1] * (r - i)
246 yield tuple(pool[i] for i in indices)
247
Raymond Hettinger30c73622010-12-01 23:45:20 +0000248 The code for :func:`combinations_with_replacement` can be also expressed as
249 a subsequence of :func:`product` after filtering entries where the elements
250 are not in sorted order (according to their position in the input pool)::
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000251
252 def combinations_with_replacement(iterable, r):
253 pool = tuple(iterable)
254 n = len(pool)
255 for indices in product(range(n), repeat=r):
256 if sorted(indices) == list(indices):
257 yield tuple(pool[i] for i in indices)
258
Raymond Hettinger30c73622010-12-01 23:45:20 +0000259 The number of items returned is ``(n+r-1)! / r! / (n-1)!`` when ``n > 0``.
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000260
Raymond Hettinger30c73622010-12-01 23:45:20 +0000261 .. versionadded:: 3.1
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000262
Georg Brandl67b21b72010-08-17 15:07:14 +0000263
Raymond Hettinger6b3b0fc2009-01-26 02:56:58 +0000264.. function:: compress(data, selectors)
265
Raymond Hettinger30c73622010-12-01 23:45:20 +0000266 Make an iterator that filters elements from *data* returning only those that
267 have a corresponding element in *selectors* that evaluates to ``True``.
268 Stops when either the *data* or *selectors* iterables has been exhausted.
269 Equivalent to::
Raymond Hettinger6b3b0fc2009-01-26 02:56:58 +0000270
Raymond Hettinger30c73622010-12-01 23:45:20 +0000271 def compress(data, selectors):
272 # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
273 return (d for d, s in zip(data, selectors) if s)
Raymond Hettinger6b3b0fc2009-01-26 02:56:58 +0000274
Raymond Hettinger30c73622010-12-01 23:45:20 +0000275 .. versionadded:: 3.1
Raymond Hettinger6b3b0fc2009-01-26 02:56:58 +0000276
277
Raymond Hettinger9e8dbbc2009-02-14 04:21:49 +0000278.. function:: count(start=0, step=1)
Georg Brandl116aa622007-08-15 14:28:22 +0000279
Andrew Kuchlingedb42602013-06-21 08:00:58 -0400280 Make an iterator that returns evenly spaced values starting with number *start*. Often
Raymond Hettinger30c73622010-12-01 23:45:20 +0000281 used as an argument to :func:`map` to generate consecutive data points.
282 Also, used with :func:`zip` to add sequence numbers. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000283
Raymond Hettinger30c73622010-12-01 23:45:20 +0000284 def count(start=0, step=1):
285 # count(10) --> 10 11 12 13 14 ...
Georg Brandl37a80dc2011-01-13 07:31:18 +0000286 # count(2.5, 0.5) -> 2.5 3.0 3.5 ...
Raymond Hettinger30c73622010-12-01 23:45:20 +0000287 n = start
288 while True:
289 yield n
290 n += step
Georg Brandl116aa622007-08-15 14:28:22 +0000291
Raymond Hettinger30c73622010-12-01 23:45:20 +0000292 When counting with floating point numbers, better accuracy can sometimes be
293 achieved by substituting multiplicative code such as: ``(start + step * i
294 for i in count())``.
Raymond Hettinger5bc472a2009-06-17 01:40:52 +0000295
Raymond Hettinger30c73622010-12-01 23:45:20 +0000296 .. versionchanged:: 3.1
297 Added *step* argument and allowed non-integer arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000298
299.. function:: cycle(iterable)
300
Raymond Hettinger30c73622010-12-01 23:45:20 +0000301 Make an iterator returning elements from the iterable and saving a copy of each.
302 When the iterable is exhausted, return elements from the saved copy. Repeats
303 indefinitely. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000304
Raymond Hettinger30c73622010-12-01 23:45:20 +0000305 def cycle(iterable):
306 # cycle('ABCD') --> A B C D A B C D A B C D ...
307 saved = []
308 for element in iterable:
309 yield element
310 saved.append(element)
311 while saved:
312 for element in saved:
Georg Brandl116aa622007-08-15 14:28:22 +0000313 yield element
314
Raymond Hettinger30c73622010-12-01 23:45:20 +0000315 Note, this member of the toolkit may require significant auxiliary storage
316 (depending on the length of the iterable).
Georg Brandl116aa622007-08-15 14:28:22 +0000317
318
319.. function:: dropwhile(predicate, iterable)
320
Raymond Hettinger30c73622010-12-01 23:45:20 +0000321 Make an iterator that drops elements from the iterable as long as the predicate
322 is true; afterwards, returns every element. Note, the iterator does not produce
323 *any* output until the predicate first becomes false, so it may have a lengthy
324 start-up time. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000325
Raymond Hettinger30c73622010-12-01 23:45:20 +0000326 def dropwhile(predicate, iterable):
327 # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
328 iterable = iter(iterable)
329 for x in iterable:
330 if not predicate(x):
331 yield x
332 break
333 for x in iterable:
334 yield x
Georg Brandl116aa622007-08-15 14:28:22 +0000335
Raymond Hettinger749761e2009-01-27 04:42:48 +0000336.. function:: filterfalse(predicate, iterable)
337
Raymond Hettinger30c73622010-12-01 23:45:20 +0000338 Make an iterator that filters elements from iterable returning only those for
339 which the predicate is ``False``. If *predicate* is ``None``, return the items
340 that are false. Equivalent to::
Raymond Hettinger749761e2009-01-27 04:42:48 +0000341
Raymond Hettinger30c73622010-12-01 23:45:20 +0000342 def filterfalse(predicate, iterable):
343 # filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
344 if predicate is None:
345 predicate = bool
346 for x in iterable:
347 if not predicate(x):
348 yield x
Raymond Hettinger749761e2009-01-27 04:42:48 +0000349
Georg Brandl116aa622007-08-15 14:28:22 +0000350
Georg Brandl3dd33882009-06-01 17:35:27 +0000351.. function:: groupby(iterable, key=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000352
Raymond Hettinger30c73622010-12-01 23:45:20 +0000353 Make an iterator that returns consecutive keys and groups from the *iterable*.
354 The *key* is a function computing a key value for each element. If not
355 specified or is ``None``, *key* defaults to an identity function and returns
356 the element unchanged. Generally, the iterable needs to already be sorted on
357 the same key function.
Georg Brandl116aa622007-08-15 14:28:22 +0000358
Raymond Hettinger30c73622010-12-01 23:45:20 +0000359 The operation of :func:`groupby` is similar to the ``uniq`` filter in Unix. It
360 generates a break or new group every time the value of the key function changes
361 (which is why it is usually necessary to have sorted the data using the same key
362 function). That behavior differs from SQL's GROUP BY which aggregates common
363 elements regardless of their input order.
Georg Brandl116aa622007-08-15 14:28:22 +0000364
Raymond Hettinger30c73622010-12-01 23:45:20 +0000365 The returned group is itself an iterator that shares the underlying iterable
366 with :func:`groupby`. Because the source is shared, when the :func:`groupby`
367 object is advanced, the previous group is no longer visible. So, if that data
368 is needed later, it should be stored as a list::
Georg Brandl116aa622007-08-15 14:28:22 +0000369
Raymond Hettinger30c73622010-12-01 23:45:20 +0000370 groups = []
371 uniquekeys = []
372 data = sorted(data, key=keyfunc)
373 for k, g in groupby(data, keyfunc):
374 groups.append(list(g)) # Store group iterator as a list
375 uniquekeys.append(k)
Georg Brandl116aa622007-08-15 14:28:22 +0000376
Raymond Hettinger30c73622010-12-01 23:45:20 +0000377 :func:`groupby` is equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000378
Raymond Hettinger30c73622010-12-01 23:45:20 +0000379 class groupby:
380 # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
381 # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
382 def __init__(self, iterable, key=None):
383 if key is None:
384 key = lambda x: x
385 self.keyfunc = key
386 self.it = iter(iterable)
387 self.tgtkey = self.currkey = self.currvalue = object()
388 def __iter__(self):
389 return self
390 def __next__(self):
391 while self.currkey == self.tgtkey:
392 self.currvalue = next(self.it) # Exit on StopIteration
393 self.currkey = self.keyfunc(self.currvalue)
394 self.tgtkey = self.currkey
395 return (self.currkey, self._grouper(self.tgtkey))
396 def _grouper(self, tgtkey):
397 while self.currkey == tgtkey:
398 yield self.currvalue
399 self.currvalue = next(self.it) # Exit on StopIteration
400 self.currkey = self.keyfunc(self.currvalue)
Georg Brandl116aa622007-08-15 14:28:22 +0000401
Georg Brandl116aa622007-08-15 14:28:22 +0000402
Ezio Melottie0add762012-09-14 06:32:35 +0300403.. function:: islice(iterable, stop)
404 islice(iterable, start, stop[, step])
Georg Brandl116aa622007-08-15 14:28:22 +0000405
Raymond Hettinger30c73622010-12-01 23:45:20 +0000406 Make an iterator that returns selected elements from the iterable. If *start* is
407 non-zero, then elements from the iterable are skipped until start is reached.
408 Afterward, elements are returned consecutively unless *step* is set higher than
409 one which results in items being skipped. If *stop* is ``None``, then iteration
410 continues until the iterator is exhausted, if at all; otherwise, it stops at the
411 specified position. Unlike regular slicing, :func:`islice` does not support
412 negative values for *start*, *stop*, or *step*. Can be used to extract related
413 fields from data where the internal structure has been flattened (for example, a
414 multi-line report may list a name field on every third line). Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000415
Raymond Hettinger30c73622010-12-01 23:45:20 +0000416 def islice(iterable, *args):
417 # islice('ABCDEFG', 2) --> A B
418 # islice('ABCDEFG', 2, 4) --> C D
419 # islice('ABCDEFG', 2, None) --> C D E F G
420 # islice('ABCDEFG', 0, None, 2) --> A C E G
421 s = slice(*args)
422 it = iter(range(s.start or 0, s.stop or sys.maxsize, s.step or 1))
423 nexti = next(it)
424 for i, element in enumerate(iterable):
425 if i == nexti:
426 yield element
427 nexti = next(it)
Georg Brandl116aa622007-08-15 14:28:22 +0000428
Raymond Hettinger30c73622010-12-01 23:45:20 +0000429 If *start* is ``None``, then iteration starts at zero. If *step* is ``None``,
430 then the step defaults to one.
Georg Brandl116aa622007-08-15 14:28:22 +0000431
Georg Brandl116aa622007-08-15 14:28:22 +0000432
Georg Brandl3dd33882009-06-01 17:35:27 +0000433.. function:: permutations(iterable, r=None)
Christian Heimes70e7ea22008-02-28 20:02:27 +0000434
Raymond Hettinger30c73622010-12-01 23:45:20 +0000435 Return successive *r* length permutations of elements in the *iterable*.
Christian Heimes70e7ea22008-02-28 20:02:27 +0000436
Raymond Hettinger30c73622010-12-01 23:45:20 +0000437 If *r* is not specified or is ``None``, then *r* defaults to the length
438 of the *iterable* and all possible full-length permutations
439 are generated.
Christian Heimes70e7ea22008-02-28 20:02:27 +0000440
Raymond Hettinger30c73622010-12-01 23:45:20 +0000441 Permutations are emitted in lexicographic sort order. So, if the
442 input *iterable* is sorted, the permutation tuples will be produced
443 in sorted order.
Christian Heimes70e7ea22008-02-28 20:02:27 +0000444
Raymond Hettinger30c73622010-12-01 23:45:20 +0000445 Elements are treated as unique based on their position, not on their
446 value. So if the input elements are unique, there will be no repeat
447 values in each permutation.
Christian Heimes70e7ea22008-02-28 20:02:27 +0000448
Raymond Hettinger30c73622010-12-01 23:45:20 +0000449 Equivalent to::
Christian Heimesb558a2e2008-03-02 22:46:37 +0000450
451 def permutations(iterable, r=None):
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000452 # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
453 # permutations(range(3)) --> 012 021 102 120 201 210
Christian Heimesb558a2e2008-03-02 22:46:37 +0000454 pool = tuple(iterable)
455 n = len(pool)
456 r = n if r is None else r
Raymond Hettinger5bad41e2009-01-08 21:01:54 +0000457 if r > n:
458 return
459 indices = list(range(n))
Sandro Tosi73866622011-12-25 17:25:45 +0100460 cycles = list(range(n, n-r, -1))
Christian Heimesb558a2e2008-03-02 22:46:37 +0000461 yield tuple(pool[i] for i in indices[:r])
462 while n:
463 for i in reversed(range(r)):
464 cycles[i] -= 1
465 if cycles[i] == 0:
466 indices[i:] = indices[i+1:] + indices[i:i+1]
467 cycles[i] = n - i
468 else:
469 j = cycles[i]
470 indices[i], indices[-j] = indices[-j], indices[i]
471 yield tuple(pool[i] for i in indices[:r])
472 break
473 else:
474 return
Christian Heimes70e7ea22008-02-28 20:02:27 +0000475
Raymond Hettinger30c73622010-12-01 23:45:20 +0000476 The code for :func:`permutations` can be also expressed as a subsequence of
477 :func:`product`, filtered to exclude entries with repeated elements (those
478 from the same position in the input pool)::
Christian Heimes78644762008-03-04 23:39:23 +0000479
480 def permutations(iterable, r=None):
481 pool = tuple(iterable)
482 n = len(pool)
483 r = n if r is None else r
484 for indices in product(range(n), repeat=r):
485 if len(set(indices)) == r:
486 yield tuple(pool[i] for i in indices)
487
Raymond Hettinger30c73622010-12-01 23:45:20 +0000488 The number of items returned is ``n! / (n-r)!`` when ``0 <= r <= n``
489 or zero when ``r > n``.
Christian Heimes70e7ea22008-02-28 20:02:27 +0000490
Georg Brandl3dd33882009-06-01 17:35:27 +0000491.. function:: product(*iterables, repeat=1)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000492
Raymond Hettinger30c73622010-12-01 23:45:20 +0000493 Cartesian product of input iterables.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000494
Raymond Hettinger30c73622010-12-01 23:45:20 +0000495 Equivalent to nested for-loops in a generator expression. For example,
496 ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000497
Raymond Hettinger30c73622010-12-01 23:45:20 +0000498 The nested loops cycle like an odometer with the rightmost element advancing
499 on every iteration. This pattern creates a lexicographic ordering so that if
500 the input's iterables are sorted, the product tuples are emitted in sorted
501 order.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000502
Raymond Hettinger30c73622010-12-01 23:45:20 +0000503 To compute the product of an iterable with itself, specify the number of
504 repetitions with the optional *repeat* keyword argument. For example,
505 ``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``.
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000506
Raymond Hettinger30c73622010-12-01 23:45:20 +0000507 This function is equivalent to the following code, except that the
508 actual implementation does not build up intermediate results in memory::
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000509
Raymond Hettinger30c73622010-12-01 23:45:20 +0000510 def product(*args, repeat=1):
511 # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
512 # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
513 pools = [tuple(pool) for pool in args] * repeat
514 result = [[]]
515 for pool in pools:
516 result = [x+[y] for x in result for y in pool]
517 for prod in result:
518 yield tuple(prod)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000519
520
Raymond Hettingerd75ad442009-06-01 19:16:52 +0000521.. function:: repeat(object[, times])
Georg Brandl116aa622007-08-15 14:28:22 +0000522
Raymond Hettinger30c73622010-12-01 23:45:20 +0000523 Make an iterator that returns *object* over and over again. Runs indefinitely
524 unless the *times* argument is specified. Used as argument to :func:`map` for
525 invariant parameters to the called function. Also used with :func:`zip` to
526 create an invariant part of a tuple record. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000527
Raymond Hettinger30c73622010-12-01 23:45:20 +0000528 def repeat(object, times=None):
529 # repeat(10, 3) --> 10 10 10
530 if times is None:
531 while True:
532 yield object
533 else:
534 for i in range(times):
535 yield object
Georg Brandl116aa622007-08-15 14:28:22 +0000536
Raymond Hettingerfc3ba6b2012-02-01 09:07:40 -0800537 A common use for *repeat* is to supply a stream of constant values to *map*
538 or *zip*::
539
540 >>> list(map(pow, range(10), repeat(2)))
541 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Georg Brandl116aa622007-08-15 14:28:22 +0000542
543.. function:: starmap(function, iterable)
544
Raymond Hettinger30c73622010-12-01 23:45:20 +0000545 Make an iterator that computes the function using arguments obtained from
546 the iterable. Used instead of :func:`map` when argument parameters are already
547 grouped in tuples from a single iterable (the data has been "pre-zipped"). The
548 difference between :func:`map` and :func:`starmap` parallels the distinction
549 between ``function(a,b)`` and ``function(*c)``. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000550
Raymond Hettinger30c73622010-12-01 23:45:20 +0000551 def starmap(function, iterable):
552 # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
553 for args in iterable:
554 yield function(*args)
Christian Heimes679db4a2008-01-18 09:56:22 +0000555
Georg Brandl116aa622007-08-15 14:28:22 +0000556
557.. function:: takewhile(predicate, iterable)
558
Raymond Hettinger30c73622010-12-01 23:45:20 +0000559 Make an iterator that returns elements from the iterable as long as the
560 predicate is true. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000561
Raymond Hettinger30c73622010-12-01 23:45:20 +0000562 def takewhile(predicate, iterable):
563 # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
564 for x in iterable:
565 if predicate(x):
566 yield x
567 else:
568 break
Georg Brandl116aa622007-08-15 14:28:22 +0000569
570
Georg Brandl3dd33882009-06-01 17:35:27 +0000571.. function:: tee(iterable, n=2)
Georg Brandl116aa622007-08-15 14:28:22 +0000572
Raymond Hettinger30c73622010-12-01 23:45:20 +0000573 Return *n* independent iterators from a single iterable. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000574
Raymond Hettingercf984ce2009-02-18 20:56:51 +0000575 def tee(iterable, n=2):
576 it = iter(iterable)
577 deques = [collections.deque() for i in range(n)]
578 def gen(mydeque):
579 while True:
580 if not mydeque: # when the local deque is empty
581 newval = next(it) # fetch a new value and
582 for d in deques: # load it to all the deques
583 d.append(newval)
584 yield mydeque.popleft()
585 return tuple(gen(d) for d in deques)
Georg Brandl116aa622007-08-15 14:28:22 +0000586
Raymond Hettinger30c73622010-12-01 23:45:20 +0000587 Once :func:`tee` has made a split, the original *iterable* should not be
588 used anywhere else; otherwise, the *iterable* could get advanced without
589 the tee objects being informed.
Georg Brandl116aa622007-08-15 14:28:22 +0000590
Raymond Hettinger30c73622010-12-01 23:45:20 +0000591 This itertool may require significant auxiliary storage (depending on how
592 much temporary data needs to be stored). In general, if one iterator uses
593 most or all of the data before another iterator starts, it is faster to use
594 :func:`list` instead of :func:`tee`.
Georg Brandl116aa622007-08-15 14:28:22 +0000595
Georg Brandl116aa622007-08-15 14:28:22 +0000596
Georg Brandl3dd33882009-06-01 17:35:27 +0000597.. function:: zip_longest(*iterables, fillvalue=None)
Raymond Hettinger749761e2009-01-27 04:42:48 +0000598
Raymond Hettinger30c73622010-12-01 23:45:20 +0000599 Make an iterator that aggregates elements from each of the iterables. If the
600 iterables are of uneven length, missing values are filled-in with *fillvalue*.
601 Iteration continues until the longest iterable is exhausted. Equivalent to::
Raymond Hettinger749761e2009-01-27 04:42:48 +0000602
Raymond Hettinger6f45d182011-10-30 15:06:14 -0700603 class ZipExhausted(Exception):
604 pass
605
606 def zip_longest(*args, **kwds):
Raymond Hettinger30c73622010-12-01 23:45:20 +0000607 # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
Raymond Hettinger6f45d182011-10-30 15:06:14 -0700608 fillvalue = kwds.get('fillvalue')
609 counter = len(args) - 1
610 def sentinel():
611 nonlocal counter
612 if not counter:
613 raise ZipExhausted
614 counter -= 1
615 yield fillvalue
Raymond Hettinger30c73622010-12-01 23:45:20 +0000616 fillers = repeat(fillvalue)
Raymond Hettinger6f45d182011-10-30 15:06:14 -0700617 iterators = [chain(it, sentinel(), fillers) for it in args]
Raymond Hettinger30c73622010-12-01 23:45:20 +0000618 try:
Raymond Hettinger6f45d182011-10-30 15:06:14 -0700619 while iterators:
620 yield tuple(map(next, iterators))
621 except ZipExhausted:
Raymond Hettinger30c73622010-12-01 23:45:20 +0000622 pass
Raymond Hettinger749761e2009-01-27 04:42:48 +0000623
Raymond Hettinger30c73622010-12-01 23:45:20 +0000624 If one of the iterables is potentially infinite, then the :func:`zip_longest`
625 function should be wrapped with something that limits the number of calls
626 (for example :func:`islice` or :func:`takewhile`). If not specified,
627 *fillvalue* defaults to ``None``.
Raymond Hettinger749761e2009-01-27 04:42:48 +0000628
629
Georg Brandl116aa622007-08-15 14:28:22 +0000630.. _itertools-recipes:
631
Raymond Hettinger1fa76822010-12-06 23:31:36 +0000632Itertools Recipes
633-----------------
Georg Brandl116aa622007-08-15 14:28:22 +0000634
635This section shows recipes for creating an extended toolset using the existing
636itertools as building blocks.
637
638The extended tools offer the same high performance as the underlying toolset.
639The superior memory performance is kept by processing elements one at a time
640rather than bringing the whole iterable into memory all at once. Code volume is
641kept small by linking the tools together in a functional style which helps
642eliminate temporary variables. High speed is retained by preferring
Georg Brandl9afde1c2007-11-01 20:32:30 +0000643"vectorized" building blocks over the use of for-loops and :term:`generator`\s
Christian Heimesfe337bf2008-03-23 21:54:12 +0000644which incur interpreter overhead.
645
646.. testcode::
Georg Brandl116aa622007-08-15 14:28:22 +0000647
Raymond Hettinger30c73622010-12-01 23:45:20 +0000648 def take(n, iterable):
649 "Return first n items of the iterable as a list"
650 return list(islice(iterable, n))
Georg Brandl116aa622007-08-15 14:28:22 +0000651
Raymond Hettinger30c73622010-12-01 23:45:20 +0000652 def tabulate(function, start=0):
653 "Return function(0), function(1), ..."
654 return map(function, count(start))
Raymond Hettingerdd1150e2008-03-13 02:39:40 +0000655
Raymond Hettinger30c73622010-12-01 23:45:20 +0000656 def consume(iterator, n):
657 "Advance the iterator n-steps ahead. If n is none, consume entirely."
658 # Use functions that consume iterators at C speed.
659 if n is None:
660 # feed the entire iterator into a zero-length deque
661 collections.deque(iterator, maxlen=0)
662 else:
663 # advance to the empty slice starting at position n
664 next(islice(iterator, n, n), None)
Raymond Hettingerfa007962009-03-09 11:55:25 +0000665
Raymond Hettinger30c73622010-12-01 23:45:20 +0000666 def nth(iterable, n, default=None):
667 "Returns the nth item or a default value"
668 return next(islice(iterable, n, None), default)
Georg Brandl116aa622007-08-15 14:28:22 +0000669
Raymond Hettinger30c73622010-12-01 23:45:20 +0000670 def quantify(iterable, pred=bool):
671 "Count how many times the predicate is true"
672 return sum(map(pred, iterable))
Georg Brandl116aa622007-08-15 14:28:22 +0000673
Raymond Hettinger30c73622010-12-01 23:45:20 +0000674 def padnone(iterable):
675 """Returns the sequence elements and then returns None indefinitely.
Georg Brandl116aa622007-08-15 14:28:22 +0000676
Raymond Hettinger30c73622010-12-01 23:45:20 +0000677 Useful for emulating the behavior of the built-in map() function.
678 """
679 return chain(iterable, repeat(None))
Georg Brandl116aa622007-08-15 14:28:22 +0000680
Raymond Hettinger30c73622010-12-01 23:45:20 +0000681 def ncycles(iterable, n):
682 "Returns the sequence elements n times"
683 return chain.from_iterable(repeat(tuple(iterable), n))
Georg Brandl116aa622007-08-15 14:28:22 +0000684
Raymond Hettinger30c73622010-12-01 23:45:20 +0000685 def dotproduct(vec1, vec2):
686 return sum(map(operator.mul, vec1, vec2))
Georg Brandl116aa622007-08-15 14:28:22 +0000687
Raymond Hettinger30c73622010-12-01 23:45:20 +0000688 def flatten(listOfLists):
689 "Flatten one level of nesting"
690 return chain.from_iterable(listOfLists)
Georg Brandl116aa622007-08-15 14:28:22 +0000691
Raymond Hettinger30c73622010-12-01 23:45:20 +0000692 def repeatfunc(func, times=None, *args):
693 """Repeat calls to func with specified arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000694
Raymond Hettinger30c73622010-12-01 23:45:20 +0000695 Example: repeatfunc(random.random)
696 """
697 if times is None:
698 return starmap(func, repeat(args))
699 return starmap(func, repeat(args, times))
Georg Brandl116aa622007-08-15 14:28:22 +0000700
Raymond Hettinger30c73622010-12-01 23:45:20 +0000701 def pairwise(iterable):
702 "s -> (s0,s1), (s1,s2), (s2, s3), ..."
703 a, b = tee(iterable)
704 next(b, None)
705 return zip(a, b)
Georg Brandl116aa622007-08-15 14:28:22 +0000706
Raymond Hettinger44571da2013-05-05 19:53:41 -0700707 def grouper(iterable, n, fillvalue=None):
Raymond Hettinger9ae94732012-07-08 16:04:03 -0700708 "Collect data into fixed-length chunks or blocks"
Raymond Hettinger44571da2013-05-05 19:53:41 -0700709 # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
Raymond Hettinger30c73622010-12-01 23:45:20 +0000710 args = [iter(iterable)] * n
711 return zip_longest(*args, fillvalue=fillvalue)
Georg Brandl116aa622007-08-15 14:28:22 +0000712
Raymond Hettinger30c73622010-12-01 23:45:20 +0000713 def roundrobin(*iterables):
714 "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
715 # Recipe credited to George Sakkis
716 pending = len(iterables)
717 nexts = cycle(iter(it).__next__ for it in iterables)
718 while pending:
719 try:
720 for next in nexts:
721 yield next()
722 except StopIteration:
723 pending -= 1
724 nexts = cycle(islice(nexts, pending))
Georg Brandl116aa622007-08-15 14:28:22 +0000725
Raymond Hettinger30c73622010-12-01 23:45:20 +0000726 def partition(pred, iterable):
727 'Use a predicate to partition entries into false entries and true entries'
728 # partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
729 t1, t2 = tee(iterable)
730 return filterfalse(pred, t1), filter(pred, t2)
Raymond Hettinger5ce0aa22010-12-01 10:49:19 +0000731
Raymond Hettinger30c73622010-12-01 23:45:20 +0000732 def powerset(iterable):
733 "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
734 s = list(iterable)
735 return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
Raymond Hettinger08d01ee2010-08-07 05:36:53 +0000736
Raymond Hettinger30c73622010-12-01 23:45:20 +0000737 def unique_everseen(iterable, key=None):
738 "List unique elements, preserving order. Remember all elements ever seen."
739 # unique_everseen('AAAABBBCCDAABBB') --> A B C D
740 # unique_everseen('ABBCcAD', str.lower) --> A B C D
741 seen = set()
742 seen_add = seen.add
743 if key is None:
744 for element in filterfalse(seen.__contains__, iterable):
745 seen_add(element)
746 yield element
747 else:
748 for element in iterable:
749 k = key(element)
750 if k not in seen:
751 seen_add(k)
752 yield element
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000753
Raymond Hettinger30c73622010-12-01 23:45:20 +0000754 def unique_justseen(iterable, key=None):
755 "List unique elements, preserving order. Remember only the element just seen."
756 # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
757 # unique_justseen('ABBCcAD', str.lower) --> A B C A D
758 return map(next, map(itemgetter(1), groupby(iterable, key)))
Raymond Hettingerad9d96b2009-01-02 21:39:07 +0000759
Raymond Hettinger30c73622010-12-01 23:45:20 +0000760 def iter_except(func, exception, first=None):
761 """ Call a function repeatedly until an exception is raised.
Raymond Hettingerfc91aa22010-03-28 18:27:13 +0000762
Raymond Hettinger30c73622010-12-01 23:45:20 +0000763 Converts a call-until-exception interface to an iterator interface.
Andrew Kuchling1d7d5802013-06-20 21:17:41 -0400764 Like builtins.iter(func, sentinel) but uses an exception instead
Raymond Hettinger30c73622010-12-01 23:45:20 +0000765 of a sentinel to end the loop.
Raymond Hettingerfc91aa22010-03-28 18:27:13 +0000766
Raymond Hettinger30c73622010-12-01 23:45:20 +0000767 Examples:
768 iter_except(functools.partial(heappop, h), IndexError) # priority queue iterator
769 iter_except(d.popitem, KeyError) # non-blocking dict iterator
770 iter_except(d.popleft, IndexError) # non-blocking deque iterator
771 iter_except(q.get_nowait, Queue.Empty) # loop over a producer Queue
772 iter_except(s.pop, KeyError) # non-blocking set iterator
Raymond Hettingerfc91aa22010-03-28 18:27:13 +0000773
Raymond Hettinger30c73622010-12-01 23:45:20 +0000774 """
775 try:
776 if first is not None:
777 yield first() # For database APIs needing an initial cast to db.first()
778 while 1:
779 yield func()
780 except exception:
781 pass
Raymond Hettingerfc91aa22010-03-28 18:27:13 +0000782
Raymond Hettinger30c73622010-12-01 23:45:20 +0000783 def random_product(*args, repeat=1):
784 "Random selection from itertools.product(*args, **kwds)"
785 pools = [tuple(pool) for pool in args] * repeat
786 return tuple(random.choice(pool) for pool in pools)
Raymond Hettingerfc91aa22010-03-28 18:27:13 +0000787
Raymond Hettinger30c73622010-12-01 23:45:20 +0000788 def random_permutation(iterable, r=None):
789 "Random selection from itertools.permutations(iterable, r)"
790 pool = tuple(iterable)
791 r = len(pool) if r is None else r
792 return tuple(random.sample(pool, r))
Raymond Hettinger063a4b62010-04-02 04:18:18 +0000793
Raymond Hettinger30c73622010-12-01 23:45:20 +0000794 def random_combination(iterable, r):
795 "Random selection from itertools.combinations(iterable, r)"
796 pool = tuple(iterable)
797 n = len(pool)
798 indices = sorted(random.sample(range(n), r))
799 return tuple(pool[i] for i in indices)
Raymond Hettinger063a4b62010-04-02 04:18:18 +0000800
Raymond Hettinger30c73622010-12-01 23:45:20 +0000801 def random_combination_with_replacement(iterable, r):
802 "Random selection from itertools.combinations_with_replacement(iterable, r)"
803 pool = tuple(iterable)
804 n = len(pool)
805 indices = sorted(random.randrange(n) for i in range(r))
806 return tuple(pool[i] for i in indices)
Raymond Hettinger063a4b62010-04-02 04:18:18 +0000807
Raymond Hettingerfc91aa22010-03-28 18:27:13 +0000808Note, many of the above recipes can be optimized by replacing global lookups
809with local variables defined as default values. For example, the
810*dotproduct* recipe can be written as::
811
Raymond Hettinger30c73622010-12-01 23:45:20 +0000812 def dotproduct(vec1, vec2, sum=sum, map=map, mul=operator.mul):
813 return sum(map(mul, vec1, vec2))