1 """Imported from the recipes section of the itertools documentation.
3 All functions taken from the recipes section of the itertools library docs
5 Some backward-compatible usability improvements have been made.
7 .. [1] http://docs.python.org/library/itertools.html#recipes
13 from collections import deque
14 from collections.abc import Sized
15 from functools import reduce
16 from itertools import (
29 from random import randrange, sample, choice
49 'polynomial_from_roots',
53 'random_combination_with_replacement',
73 def take(n, iterable):
74 """Return first *n* items of the iterable as a list.
76 >>> take(3, range(10))
79 If there are fewer than *n* items in the iterable, all of them are
82 >>> take(10, range(3))
86 return list(islice(iterable, n))
89 def tabulate(function, start=0):
90 """Return an iterator over the results of ``func(start)``,
91 ``func(start + 1)``, ``func(start + 2)``...
93 *func* should be a function that accepts one integer argument.
95 If *start* is not specified it defaults to 0. It will be incremented each
96 time the iterator is advanced.
98 >>> square = lambda x: x ** 2
99 >>> iterator = tabulate(square, -3)
100 >>> take(4, iterator)
104 return map(function, count(start))
107 def tail(n, iterable):
108 """Return an iterator over the last *n* items of *iterable*.
110 >>> t = tail(3, 'ABCDEFG')
115 # If the given iterable has a length, then we can use islice to get its
116 # final elements. Note that if the iterable is not actually Iterable,
117 # either islice or deque will throw a TypeError. This is why we don't
118 # check if it is Iterable.
119 if isinstance(iterable, Sized):
120 yield from islice(iterable, max(0, len(iterable) - n), None)
122 yield from iter(deque(iterable, maxlen=n))
125 def consume(iterator, n=None):
126 """Advance *iterable* by *n* steps. If *n* is ``None``, consume it
129 Efficiently exhausts an iterator without returning values. Defaults to
130 consuming the whole iterator, but an optional second argument may be
131 provided to limit consumption.
133 >>> i = (x for x in range(10))
141 Traceback (most recent call last):
142 File "<stdin>", line 1, in <module>
145 If the iterator has fewer items remaining than the provided limit, the
146 whole iterator will be consumed.
148 >>> i = (x for x in range(3))
151 Traceback (most recent call last):
152 File "<stdin>", line 1, in <module>
156 # Use functions that consume iterators at C speed.
158 # feed the entire iterator into a zero-length deque
159 deque(iterator, maxlen=0)
161 # advance to the empty slice starting at position n
162 next(islice(iterator, n, n), None)
165 def nth(iterable, n, default=None):
166 """Returns the nth item or a default value.
171 >>> nth(l, 20, "zebra")
175 return next(islice(iterable, n, None), default)
178 def all_equal(iterable):
180 Returns ``True`` if all the elements are equal to each other.
182 >>> all_equal('aaaa')
184 >>> all_equal('aaab')
188 g = groupby(iterable)
189 return next(g, True) and not next(g, False)
192 def quantify(iterable, pred=bool):
193 """Return the how many times the predicate is true.
195 >>> quantify([True, False, True])
199 return sum(map(pred, iterable))
202 def pad_none(iterable):
203 """Returns the sequence of elements and then returns ``None`` indefinitely.
205 >>> take(5, pad_none(range(3)))
206 [0, 1, 2, None, None]
208 Useful for emulating the behavior of the built-in :func:`map` function.
210 See also :func:`padded`.
213 return chain(iterable, repeat(None))
219 def ncycles(iterable, n):
220 """Returns the sequence elements *n* times
222 >>> list(ncycles(["a", "b"], 3))
223 ['a', 'b', 'a', 'b', 'a', 'b']
226 return chain.from_iterable(repeat(tuple(iterable), n))
229 def dotproduct(vec1, vec2):
230 """Returns the dot product of the two iterables.
232 >>> dotproduct([10, 10], [20, 20])
236 return sum(map(operator.mul, vec1, vec2))
239 def flatten(listOfLists):
240 """Return an iterator flattening one level of nesting in a list of lists.
242 >>> list(flatten([[0, 1], [2, 3]]))
245 See also :func:`collapse`, which can flatten multiple levels of nesting.
248 return chain.from_iterable(listOfLists)
251 def repeatfunc(func, times=None, *args):
252 """Call *func* with *args* repeatedly, returning an iterable over the
255 If *times* is specified, the iterable will terminate after that many
258 >>> from operator import add
261 >>> list(repeatfunc(add, times, *args))
264 If *times* is ``None`` the iterable will not terminate:
266 >>> from random import randrange
269 >>> take(6, repeatfunc(randrange, times, *args)) # doctest:+SKIP
274 return starmap(func, repeat(args))
275 return starmap(func, repeat(args, times))
278 def _pairwise(iterable):
279 """Returns an iterator of paired items, overlapping, from the original
281 >>> take(4, pairwise(count()))
282 [(0, 1), (1, 2), (2, 3), (3, 4)]
284 On Python 3.10 and above, this is an alias for :func:`itertools.pairwise`.
293 from itertools import pairwise as itertools_pairwise
298 def pairwise(iterable):
299 yield from itertools_pairwise(iterable)
301 pairwise.__doc__ = _pairwise.__doc__
304 class UnequalIterablesError(ValueError):
305 def __init__(self, details=None):
306 msg = 'Iterables have different lengths'
307 if details is not None:
308 msg += (': index 0 has length {}; index {} has length {}').format(
312 super().__init__(msg)
315 def _zip_equal_generator(iterables):
316 for combo in zip_longest(*iterables, fillvalue=_marker):
319 raise UnequalIterablesError()
323 def _zip_equal(*iterables):
324 # Check whether the iterables are all the same size.
326 first_size = len(iterables[0])
327 for i, it in enumerate(iterables[1:], 1):
329 if size != first_size:
332 # If we didn't break out, we can use the built-in zip.
333 return zip(*iterables)
335 # If we did break out, there was a mismatch.
336 raise UnequalIterablesError(details=(first_size, i, size))
337 # If any one of the iterables didn't have a length, start reading
338 # them until one runs out.
340 return _zip_equal_generator(iterables)
343 def grouper(iterable, n, incomplete='fill', fillvalue=None):
344 """Group elements from *iterable* into fixed-length groups of length *n*.
346 >>> list(grouper('ABCDEF', 3))
347 [('A', 'B', 'C'), ('D', 'E', 'F')]
349 The keyword arguments *incomplete* and *fillvalue* control what happens for
350 iterables whose length is not a multiple of *n*.
352 When *incomplete* is `'fill'`, the last group will contain instances of
355 >>> list(grouper('ABCDEFG', 3, incomplete='fill', fillvalue='x'))
356 [('A', 'B', 'C'), ('D', 'E', 'F'), ('G', 'x', 'x')]
358 When *incomplete* is `'ignore'`, the last group will not be emitted.
360 >>> list(grouper('ABCDEFG', 3, incomplete='ignore', fillvalue='x'))
361 [('A', 'B', 'C'), ('D', 'E', 'F')]
363 When *incomplete* is `'strict'`, a subclass of `ValueError` will be raised.
365 >>> it = grouper('ABCDEFG', 3, incomplete='strict')
366 >>> list(it) # doctest: +IGNORE_EXCEPTION_DETAIL
367 Traceback (most recent call last):
369 UnequalIterablesError
372 args = [iter(iterable)] * n
373 if incomplete == 'fill':
374 return zip_longest(*args, fillvalue=fillvalue)
375 if incomplete == 'strict':
376 return _zip_equal(*args)
377 if incomplete == 'ignore':
380 raise ValueError('Expected fill, strict, or ignore')
383 def roundrobin(*iterables):
384 """Yields an item from each iterable, alternating between them.
386 >>> list(roundrobin('ABC', 'D', 'EF'))
387 ['A', 'D', 'E', 'B', 'F', 'C']
389 This function produces the same output as :func:`interleave_longest`, but
390 may perform better for some inputs (in particular when the number of
394 # Recipe credited to George Sakkis
395 pending = len(iterables)
396 nexts = cycle(iter(it).__next__ for it in iterables)
401 except StopIteration:
403 nexts = cycle(islice(nexts, pending))
406 def partition(pred, iterable):
408 Returns a 2-tuple of iterables derived from the input iterable.
409 The first yields the items that have ``pred(item) == False``.
410 The second yields the items that have ``pred(item) == True``.
412 >>> is_odd = lambda x: x % 2 != 0
413 >>> iterable = range(10)
414 >>> even_items, odd_items = partition(is_odd, iterable)
415 >>> list(even_items), list(odd_items)
416 ([0, 2, 4, 6, 8], [1, 3, 5, 7, 9])
418 If *pred* is None, :func:`bool` is used.
420 >>> iterable = [0, 1, False, True, '', ' ']
421 >>> false_items, true_items = partition(None, iterable)
422 >>> list(false_items), list(true_items)
423 ([0, False, ''], [1, True, ' '])
429 evaluations = ((pred(x), x) for x in iterable)
430 t1, t2 = tee(evaluations)
432 (x for (cond, x) in t1 if not cond),
433 (x for (cond, x) in t2 if cond),
437 def powerset(iterable):
438 """Yields all possible subsets of the iterable.
440 >>> list(powerset([1, 2, 3]))
441 [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
443 :func:`powerset` will operate on iterables that aren't :class:`set`
444 instances, so repeated elements in the input will produce repeated elements
445 in the output. Use :func:`unique_everseen` on the input to avoid generating
449 >>> list(powerset(seq))
450 [(), (1,), (1,), (0,), (1, 1), (1, 0), (1, 0), (1, 1, 0)]
451 >>> from more_itertools import unique_everseen
452 >>> list(powerset(unique_everseen(seq)))
453 [(), (1,), (0,), (1, 0)]
457 return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
460 def unique_everseen(iterable, key=None):
462 Yield unique elements, preserving order.
464 >>> list(unique_everseen('AAAABBBCCDAABBB'))
466 >>> list(unique_everseen('ABBCcAD', str.lower))
469 Sequences with a mix of hashable and unhashable items can be used.
470 The function will be slower (i.e., `O(n^2)`) for unhashable items.
472 Remember that ``list`` objects are unhashable - you can use the *key*
473 parameter to transform the list to a tuple (which is hashable) to
476 >>> iterable = ([1, 2], [2, 3], [1, 2])
477 >>> list(unique_everseen(iterable)) # Slow
479 >>> list(unique_everseen(iterable, key=tuple)) # Faster
482 Similary, you may want to convert unhashable ``set`` objects with
483 ``key=frozenset``. For ``dict`` objects,
484 ``key=lambda x: frozenset(x.items())`` can be used.
488 seenset_add = seenset.add
490 seenlist_add = seenlist.append
491 use_key = key is not None
493 for element in iterable:
494 k = key(element) if use_key else element
500 if k not in seenlist:
505 def unique_justseen(iterable, key=None):
506 """Yields elements in order, ignoring serial duplicates
508 >>> list(unique_justseen('AAAABBBCCDAABBB'))
509 ['A', 'B', 'C', 'D', 'A', 'B']
510 >>> list(unique_justseen('ABBCcAD', str.lower))
511 ['A', 'B', 'C', 'A', 'D']
514 return map(next, map(operator.itemgetter(1), groupby(iterable, key)))
517 def iter_except(func, exception, first=None):
518 """Yields results from a function repeatedly until an exception is raised.
520 Converts a call-until-exception interface to an iterator interface.
521 Like ``iter(func, sentinel)``, but uses an exception instead of a sentinel
525 >>> list(iter_except(l.pop, IndexError))
528 Multiple exceptions can be specified as a stopping condition:
530 >>> l = [1, 2, 3, '...', 4, 5, 6]
531 >>> list(iter_except(lambda: 1 + l.pop(), (IndexError, TypeError)))
533 >>> list(iter_except(lambda: 1 + l.pop(), (IndexError, TypeError)))
535 >>> list(iter_except(lambda: 1 + l.pop(), (IndexError, TypeError)))
540 if first is not None:
548 def first_true(iterable, default=None, pred=None):
550 Returns the first true value in the iterable.
552 If no true value is found, returns *default*
554 If *pred* is not None, returns the first item for which
555 ``pred(item) == True`` .
557 >>> first_true(range(10))
559 >>> first_true(range(10), pred=lambda x: x > 5)
561 >>> first_true(range(10), default='missing', pred=lambda x: x > 9)
565 return next(filter(pred, iterable), default)
568 def random_product(*args, repeat=1):
569 """Draw an item at random from each of the input iterables.
571 >>> random_product('abc', range(4), 'XYZ') # doctest:+SKIP
574 If *repeat* is provided as a keyword argument, that many items will be
575 drawn from each iterable.
577 >>> random_product('abcd', range(4), repeat=2) # doctest:+SKIP
580 This equivalent to taking a random selection from
581 ``itertools.product(*args, **kwarg)``.
584 pools = [tuple(pool) for pool in args] * repeat
585 return tuple(choice(pool) for pool in pools)
588 def random_permutation(iterable, r=None):
589 """Return a random *r* length permutation of the elements in *iterable*.
591 If *r* is not specified or is ``None``, then *r* defaults to the length of
594 >>> random_permutation(range(5)) # doctest:+SKIP
597 This equivalent to taking a random selection from
598 ``itertools.permutations(iterable, r)``.
601 pool = tuple(iterable)
602 r = len(pool) if r is None else r
603 return tuple(sample(pool, r))
606 def random_combination(iterable, r):
607 """Return a random *r* length subsequence of the elements in *iterable*.
609 >>> random_combination(range(5), 3) # doctest:+SKIP
612 This equivalent to taking a random selection from
613 ``itertools.combinations(iterable, r)``.
616 pool = tuple(iterable)
618 indices = sorted(sample(range(n), r))
619 return tuple(pool[i] for i in indices)
622 def random_combination_with_replacement(iterable, r):
623 """Return a random *r* length subsequence of elements in *iterable*,
624 allowing individual elements to be repeated.
626 >>> random_combination_with_replacement(range(3), 5) # doctest:+SKIP
629 This equivalent to taking a random selection from
630 ``itertools.combinations_with_replacement(iterable, r)``.
633 pool = tuple(iterable)
635 indices = sorted(randrange(n) for i in range(r))
636 return tuple(pool[i] for i in indices)
639 def nth_combination(iterable, r, index):
640 """Equivalent to ``list(combinations(iterable, r))[index]``.
642 The subsequences of *iterable* that are of length *r* can be ordered
643 lexicographically. :func:`nth_combination` computes the subsequence at
644 sort position *index* directly, without computing the previous
647 >>> nth_combination(range(5), 3, 5)
650 ``ValueError`` will be raised If *r* is negative or greater than the length
652 ``IndexError`` will be raised if the given *index* is invalid.
654 pool = tuple(iterable)
656 if (r < 0) or (r > n):
661 for i in range(1, k + 1):
662 c = c * (n - k + i) // i
667 if (index < 0) or (index >= c):
672 c, n, r = c * r // n, n - 1, r - 1
675 c, n = c * (n - r) // n, n - 1
676 result.append(pool[-1 - n])
681 def prepend(value, iterator):
682 """Yield *value*, followed by the elements in *iterator*.
685 >>> iterator = ['1', '2', '3']
686 >>> list(prepend(value, iterator))
689 To prepend multiple values, see :func:`itertools.chain`
690 or :func:`value_chain`.
693 return chain([value], iterator)
696 def convolve(signal, kernel):
697 """Convolve the iterable *signal* with the iterable *kernel*.
699 >>> signal = (1, 2, 3, 4, 5)
700 >>> kernel = [3, 2, 1]
701 >>> list(convolve(signal, kernel))
702 [3, 8, 14, 20, 26, 14, 5]
704 Note: the input arguments are not interchangeable, as the *kernel*
705 is immediately consumed and stored.
708 kernel = tuple(kernel)[::-1]
710 window = deque([0], maxlen=n) * n
711 for x in chain(signal, repeat(0, n - 1)):
713 yield sum(map(operator.mul, kernel, window))
716 def before_and_after(predicate, it):
717 """A variant of :func:`takewhile` that allows complete access to the
718 remainder of the iterator.
720 >>> it = iter('ABCdEfGhI')
721 >>> all_upper, remainder = before_and_after(str.isupper, it)
722 >>> ''.join(all_upper)
724 >>> ''.join(remainder) # takewhile() would lose the 'd'
727 Note that the first iterator must be fully consumed before the second
728 iterator can generate valid results.
738 transition.append(elem)
741 # Note: this is different from itertools recipes to allow nesting
742 # before_and_after remainders into before_and_after again. See tests
744 remainder_iterator = chain(transition, it)
746 return true_iterator(), remainder_iterator
749 def triplewise(iterable):
750 """Return overlapping triplets from *iterable*.
752 >>> list(triplewise('ABCDE'))
753 [('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E')]
756 for (a, _), (b, c) in pairwise(pairwise(iterable)):
760 def sliding_window(iterable, n):
761 """Return a sliding window of width *n* over *iterable*.
763 >>> list(sliding_window(range(6), 4))
764 [(0, 1, 2, 3), (1, 2, 3, 4), (2, 3, 4, 5)]
766 If *iterable* has fewer than *n* items, then nothing is yielded:
768 >>> list(sliding_window(range(3), 4))
771 For a variant with more features, see :func:`windowed`.
774 window = deque(islice(it, n), maxlen=n)
782 def subslices(iterable):
783 """Return all contiguous non-empty subslices of *iterable*.
785 >>> list(subslices('ABC'))
786 [['A'], ['A', 'B'], ['A', 'B', 'C'], ['B'], ['B', 'C'], ['C']]
788 This is similar to :func:`substrings`, but emits items in a different
792 slices = starmap(slice, combinations(range(len(seq) + 1), 2))
793 return map(operator.getitem, repeat(seq), slices)
796 def polynomial_from_roots(roots):
797 """Compute a polynomial's coefficients from its roots.
799 >>> roots = [5, -4, 3] # (x - 5) * (x + 4) * (x - 3)
800 >>> polynomial_from_roots(roots) # x^3 - 4 * x^2 - 17 * x + 60
803 # Use math.prod for Python 3.8+,
804 prod = getattr(math, 'prod', lambda x: reduce(operator.mul, x, 1))
805 roots = list(map(operator.neg, roots))
807 sum(map(prod, combinations(roots, k))) for k in range(len(roots) + 1)
812 """Yield the primes less than n.
815 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
817 isqrt = getattr(math, 'isqrt', lambda x: int(math.sqrt(x)))
819 data = bytearray([1]) * n
821 for p in compress(range(limit), data):
822 data[p + p : n : p] = bytearray(len(range(p + p, n, p)))
824 return compress(count(), data)
827 def batched(iterable, n):
828 """Batch data into lists of length *n*. The last batch may be shorter.
830 >>> list(batched('ABCDEFG', 3))
831 [['A', 'B', 'C'], ['D', 'E', 'F'], ['G']]
833 This recipe is from the ``itertools`` docs. This library also provides
834 :func:`chunked`, which has a different implementation.
838 batch = list(islice(it, n))