1:mod:`!itertools` --- Functions creating iterators for efficient looping 2======================================================================== 3 4.. module:: itertools 5 :synopsis: Functions creating iterators for efficient looping. 6 7.. moduleauthor:: Raymond Hettinger <python@rcn.com> 8.. sectionauthor:: Raymond Hettinger <python@rcn.com> 9 10.. testsetup:: 11 12 from itertools import * 13 import collections 14 import math 15 import operator 16 import random 17 18-------------- 19 20This module implements a number of :term:`iterator` building blocks inspired 21by constructs from APL, Haskell, and SML. Each has been recast in a form 22suitable for Python. 23 24The module standardizes a core set of fast, memory efficient tools that are 25useful by themselves or in combination. Together, they form an "iterator 26algebra" making it possible to construct specialized tools succinctly and 27efficiently in pure Python. 28 29For instance, SML provides a tabulation tool: ``tabulate(f)`` which produces a 30sequence ``f(0), f(1), ...``. The same effect can be achieved in Python 31by combining :func:`map` and :func:`count` to form ``map(f, count())``. 32 33These tools and their built-in counterparts also work well with the high-speed 34functions in the :mod:`operator` module. For example, the multiplication 35operator can be mapped across two vectors to form an efficient dot-product: 36``sum(starmap(operator.mul, zip(vec1, vec2, strict=True)))``. 37 38 39**Infinite iterators:** 40 41================== ================= ================================================= ========================================= 42Iterator Arguments Results Example 43================== ================= ================================================= ========================================= 44:func:`count` [start[, step]] start, start+step, start+2*step, ... ``count(10) → 10 11 12 13 14 ...`` 45:func:`cycle` p p0, p1, ... plast, p0, p1, ... ``cycle('ABCD') → A B C D A B C D ...`` 46:func:`repeat` elem [,n] elem, elem, elem, ... endlessly or up to n times ``repeat(10, 3) → 10 10 10`` 47================== ================= ================================================= ========================================= 48 49**Iterators terminating on the shortest input sequence:** 50 51============================ ============================ ================================================= ============================================================= 52Iterator Arguments Results Example 53============================ ============================ ================================================= ============================================================= 54:func:`accumulate` p [,func] p0, p0+p1, p0+p1+p2, ... ``accumulate([1,2,3,4,5]) → 1 3 6 10 15`` 55:func:`batched` p, n (p0, p1, ..., p_n-1), ... ``batched('ABCDEFG', n=3) → ABC DEF G`` 56:func:`chain` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') → A B C D E F`` 57:func:`chain.from_iterable` iterable p0, p1, ... plast, q0, q1, ... ``chain.from_iterable(['ABC', 'DEF']) → A B C D E F`` 58: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`` 59:func:`dropwhile` predicate, seq seq[n], seq[n+1], starting when predicate fails ``dropwhile(lambda x: x<5, [1,4,6,3,8]) → 6 3 8`` 60:func:`filterfalse` predicate, seq elements of seq where predicate(elem) fails ``filterfalse(lambda x: x<5, [1,4,6,3,8]) → 6 8`` 61:func:`groupby` iterable[, key] sub-iterators grouped by value of key(v) ``groupby(['A','B','DEF'], len) → (1, A B) (3, DEF)`` 62:func:`islice` seq, [start,] stop [, step] elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) → C D E F G`` 63:func:`pairwise` iterable (p[0], p[1]), (p[1], p[2]) ``pairwise('ABCDEFG') → AB BC CD DE EF FG`` 64:func:`starmap` func, seq func(\*seq[0]), func(\*seq[1]), ... ``starmap(pow, [(2,5), (3,2), (10,3)]) → 32 9 1000`` 65:func:`takewhile` predicate, seq seq[0], seq[1], until predicate fails ``takewhile(lambda x: x<5, [1,4,6,3,8]) → 1 4`` 66:func:`tee` it, n it1, it2, ... itn splits one iterator into n 67:func:`zip_longest` p, q, ... (p[0], q[0]), (p[1], q[1]), ... ``zip_longest('ABCD', 'xy', fillvalue='-') → Ax By C- D-`` 68============================ ============================ ================================================= ============================================================= 69 70**Combinatoric iterators:** 71 72============================================== ==================== ============================================================= 73Iterator Arguments Results 74============================================== ==================== ============================================================= 75:func:`product` p, q, ... [repeat=1] cartesian product, equivalent to a nested for-loop 76:func:`permutations` p[, r] r-length tuples, all possible orderings, no repeated elements 77:func:`combinations` p, r r-length tuples, in sorted order, no repeated elements 78:func:`combinations_with_replacement` p, r r-length tuples, in sorted order, with repeated elements 79============================================== ==================== ============================================================= 80 81============================================== ============================================================= 82Examples Results 83============================================== ============================================================= 84``product('ABCD', repeat=2)`` ``AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD`` 85``permutations('ABCD', 2)`` ``AB AC AD BA BC BD CA CB CD DA DB DC`` 86``combinations('ABCD', 2)`` ``AB AC AD BC BD CD`` 87``combinations_with_replacement('ABCD', 2)`` ``AA AB AC AD BB BC BD CC CD DD`` 88============================================== ============================================================= 89 90 91.. _itertools-functions: 92 93Itertool Functions 94------------------ 95 96The following functions all construct and return iterators. Some provide 97streams of infinite length, so they should only be accessed by functions or 98loops that truncate the stream. 99 100 101.. function:: accumulate(iterable[, function, *, initial=None]) 102 103 Make an iterator that returns accumulated sums or accumulated 104 results from other binary functions. 105 106 The *function* defaults to addition. The *function* should accept 107 two arguments, an accumulated total and a value from the *iterable*. 108 109 If an *initial* value is provided, the accumulation will start with 110 that value and the output will have one more element than the input 111 iterable. 112 113 Roughly equivalent to:: 114 115 def accumulate(iterable, function=operator.add, *, initial=None): 116 'Return running totals' 117 # accumulate([1,2,3,4,5]) → 1 3 6 10 15 118 # accumulate([1,2,3,4,5], initial=100) → 100 101 103 106 110 115 119 # accumulate([1,2,3,4,5], operator.mul) → 1 2 6 24 120 120 121 iterator = iter(iterable) 122 total = initial 123 if initial is None: 124 try: 125 total = next(iterator) 126 except StopIteration: 127 return 128 129 yield total 130 for element in iterator: 131 total = function(total, element) 132 yield total 133 134 To compute a running minimum, set *function* to :func:`min`. 135 For a running maximum, set *function* to :func:`max`. 136 Or for a running product, set *function* to :func:`operator.mul`. 137 To build an `amortization table 138 <https://www.ramseysolutions.com/real-estate/amortization-schedule>`_, 139 accumulate the interest and apply payments: 140 141 .. doctest:: 142 143 >>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8] 144 >>> list(accumulate(data, max)) # running maximum 145 [3, 4, 6, 6, 6, 9, 9, 9, 9, 9] 146 >>> list(accumulate(data, operator.mul)) # running product 147 [3, 12, 72, 144, 144, 1296, 0, 0, 0, 0] 148 149 # Amortize a 5% loan of 1000 with 10 annual payments of 90 150 >>> update = lambda balance, payment: round(balance * 1.05) - payment 151 >>> list(accumulate(repeat(90, 10), update, initial=1_000)) 152 [1000, 960, 918, 874, 828, 779, 728, 674, 618, 559, 497] 153 154 See :func:`functools.reduce` for a similar function that returns only the 155 final accumulated value. 156 157 .. versionadded:: 3.2 158 159 .. versionchanged:: 3.3 160 Added the optional *function* parameter. 161 162 .. versionchanged:: 3.8 163 Added the optional *initial* parameter. 164 165 166.. function:: batched(iterable, n, *, strict=False) 167 168 Batch data from the *iterable* into tuples of length *n*. The last 169 batch may be shorter than *n*. 170 171 If *strict* is true, will raise a :exc:`ValueError` if the final 172 batch is shorter than *n*. 173 174 Loops over the input iterable and accumulates data into tuples up to 175 size *n*. The input is consumed lazily, just enough to fill a batch. 176 The result is yielded as soon as the batch is full or when the input 177 iterable is exhausted: 178 179 .. doctest:: 180 181 >>> flattened_data = ['roses', 'red', 'violets', 'blue', 'sugar', 'sweet'] 182 >>> unflattened = list(batched(flattened_data, 2)) 183 >>> unflattened 184 [('roses', 'red'), ('violets', 'blue'), ('sugar', 'sweet')] 185 186 Roughly equivalent to:: 187 188 def batched(iterable, n, *, strict=False): 189 # batched('ABCDEFG', 3) → ABC DEF G 190 if n < 1: 191 raise ValueError('n must be at least one') 192 iterator = iter(iterable) 193 while batch := tuple(islice(iterator, n)): 194 if strict and len(batch) != n: 195 raise ValueError('batched(): incomplete batch') 196 yield batch 197 198 .. versionadded:: 3.12 199 200 .. versionchanged:: 3.13 201 Added the *strict* option. 202 203 204.. function:: chain(*iterables) 205 206 Make an iterator that returns elements from the first iterable until 207 it is exhausted, then proceeds to the next iterable, until all of the 208 iterables are exhausted. This combines multiple data sources into a 209 single iterator. Roughly equivalent to:: 210 211 def chain(*iterables): 212 # chain('ABC', 'DEF') → A B C D E F 213 for iterable in iterables: 214 yield from iterable 215 216 217.. classmethod:: chain.from_iterable(iterable) 218 219 Alternate constructor for :func:`chain`. Gets chained inputs from a 220 single iterable argument that is evaluated lazily. Roughly equivalent to:: 221 222 def from_iterable(iterables): 223 # chain.from_iterable(['ABC', 'DEF']) → A B C D E F 224 for iterable in iterables: 225 yield from iterable 226 227 228.. function:: combinations(iterable, r) 229 230 Return *r* length subsequences of elements from the input *iterable*. 231 232 The output is a subsequence of :func:`product` keeping only entries that 233 are subsequences of the *iterable*. The length of the output is given 234 by :func:`math.comb` which computes ``n! / r! / (n - r)!`` when ``0 ≤ r 235 ≤ n`` or zero when ``r > n``. 236 237 The combination tuples are emitted in lexicographic order according to 238 the order of the input *iterable*. If the input *iterable* is sorted, 239 the output tuples will be produced in sorted order. 240 241 Elements are treated as unique based on their position, not on their 242 value. If the input elements are unique, there will be no repeated 243 values within each combination. 244 245 Roughly equivalent to:: 246 247 def combinations(iterable, r): 248 # combinations('ABCD', 2) → AB AC AD BC BD CD 249 # combinations(range(4), 3) → 012 013 023 123 250 251 pool = tuple(iterable) 252 n = len(pool) 253 if r > n: 254 return 255 indices = list(range(r)) 256 257 yield tuple(pool[i] for i in indices) 258 while True: 259 for i in reversed(range(r)): 260 if indices[i] != i + n - r: 261 break 262 else: 263 return 264 indices[i] += 1 265 for j in range(i+1, r): 266 indices[j] = indices[j-1] + 1 267 yield tuple(pool[i] for i in indices) 268 269 270.. function:: combinations_with_replacement(iterable, r) 271 272 Return *r* length subsequences of elements from the input *iterable* 273 allowing individual elements to be repeated more than once. 274 275 The output is a subsequence of :func:`product` that keeps only entries 276 that are subsequences (with possible repeated elements) of the 277 *iterable*. The number of subsequence returned is ``(n + r - 1)! / r! / 278 (n - 1)!`` when ``n > 0``. 279 280 The combination tuples are emitted in lexicographic order according to 281 the order of the input *iterable*. if the input *iterable* is sorted, 282 the output tuples will be produced in sorted order. 283 284 Elements are treated as unique based on their position, not on their 285 value. If the input elements are unique, the generated combinations 286 will also be unique. 287 288 Roughly equivalent to:: 289 290 def combinations_with_replacement(iterable, r): 291 # combinations_with_replacement('ABC', 2) → AA AB AC BB BC CC 292 293 pool = tuple(iterable) 294 n = len(pool) 295 if not n and r: 296 return 297 indices = [0] * r 298 299 yield tuple(pool[i] for i in indices) 300 while True: 301 for i in reversed(range(r)): 302 if indices[i] != n - 1: 303 break 304 else: 305 return 306 indices[i:] = [indices[i] + 1] * (r - i) 307 yield tuple(pool[i] for i in indices) 308 309 .. versionadded:: 3.1 310 311 312.. function:: compress(data, selectors) 313 314 Make an iterator that returns elements from *data* where the 315 corresponding element in *selectors* is true. Stops when either the 316 *data* or *selectors* iterables have been exhausted. Roughly 317 equivalent to:: 318 319 def compress(data, selectors): 320 # compress('ABCDEF', [1,0,1,0,1,1]) → A C E F 321 return (datum for datum, selector in zip(data, selectors) if selector) 322 323 .. versionadded:: 3.1 324 325 326.. function:: count(start=0, step=1) 327 328 Make an iterator that returns evenly spaced values beginning with 329 *start*. Can be used with :func:`map` to generate consecutive data 330 points or with :func:`zip` to add sequence numbers. Roughly 331 equivalent to:: 332 333 def count(start=0, step=1): 334 # count(10) → 10 11 12 13 14 ... 335 # count(2.5, 0.5) → 2.5 3.0 3.5 ... 336 n = start 337 while True: 338 yield n 339 n += step 340 341 When counting with floating-point numbers, better accuracy can sometimes be 342 achieved by substituting multiplicative code such as: ``(start + step * i 343 for i in count())``. 344 345 .. versionchanged:: 3.1 346 Added *step* argument and allowed non-integer arguments. 347 348 349.. function:: cycle(iterable) 350 351 Make an iterator returning elements from the *iterable* and saving a 352 copy of each. When the iterable is exhausted, return elements from 353 the saved copy. Repeats indefinitely. Roughly equivalent to:: 354 355 def cycle(iterable): 356 # cycle('ABCD') → A B C D A B C D A B C D ... 357 358 saved = [] 359 for element in iterable: 360 yield element 361 saved.append(element) 362 363 while saved: 364 for element in saved: 365 yield element 366 367 This itertool may require significant auxiliary storage (depending on 368 the length of the iterable). 369 370 371.. function:: dropwhile(predicate, iterable) 372 373 Make an iterator that drops elements from the *iterable* while the 374 *predicate* is true and afterwards returns every element. Roughly 375 equivalent to:: 376 377 def dropwhile(predicate, iterable): 378 # dropwhile(lambda x: x<5, [1,4,6,3,8]) → 6 3 8 379 380 iterator = iter(iterable) 381 for x in iterator: 382 if not predicate(x): 383 yield x 384 break 385 386 for x in iterator: 387 yield x 388 389 Note this does not produce *any* output until the predicate first 390 becomes false, so this itertool may have a lengthy start-up time. 391 392 393.. function:: filterfalse(predicate, iterable) 394 395 Make an iterator that filters elements from the *iterable* returning 396 only those for which the *predicate* returns a false value. If 397 *predicate* is ``None``, returns the items that are false. Roughly 398 equivalent to:: 399 400 def filterfalse(predicate, iterable): 401 # filterfalse(lambda x: x<5, [1,4,6,3,8]) → 6 8 402 403 if predicate is None: 404 predicate = bool 405 406 for x in iterable: 407 if not predicate(x): 408 yield x 409 410 411.. function:: groupby(iterable, key=None) 412 413 Make an iterator that returns consecutive keys and groups from the *iterable*. 414 The *key* is a function computing a key value for each element. If not 415 specified or is ``None``, *key* defaults to an identity function and returns 416 the element unchanged. Generally, the iterable needs to already be sorted on 417 the same key function. 418 419 The operation of :func:`groupby` is similar to the ``uniq`` filter in Unix. It 420 generates a break or new group every time the value of the key function changes 421 (which is why it is usually necessary to have sorted the data using the same key 422 function). That behavior differs from SQL's GROUP BY which aggregates common 423 elements regardless of their input order. 424 425 The returned group is itself an iterator that shares the underlying iterable 426 with :func:`groupby`. Because the source is shared, when the :func:`groupby` 427 object is advanced, the previous group is no longer visible. So, if that data 428 is needed later, it should be stored as a list:: 429 430 groups = [] 431 uniquekeys = [] 432 data = sorted(data, key=keyfunc) 433 for k, g in groupby(data, keyfunc): 434 groups.append(list(g)) # Store group iterator as a list 435 uniquekeys.append(k) 436 437 :func:`groupby` is roughly equivalent to:: 438 439 def groupby(iterable, key=None): 440 # [k for k, g in groupby('AAAABBBCCDAABBB')] → A B C D A B 441 # [list(g) for k, g in groupby('AAAABBBCCD')] → AAAA BBB CC D 442 443 keyfunc = (lambda x: x) if key is None else key 444 iterator = iter(iterable) 445 exhausted = False 446 447 def _grouper(target_key): 448 nonlocal curr_value, curr_key, exhausted 449 yield curr_value 450 for curr_value in iterator: 451 curr_key = keyfunc(curr_value) 452 if curr_key != target_key: 453 return 454 yield curr_value 455 exhausted = True 456 457 try: 458 curr_value = next(iterator) 459 except StopIteration: 460 return 461 curr_key = keyfunc(curr_value) 462 463 while not exhausted: 464 target_key = curr_key 465 curr_group = _grouper(target_key) 466 yield curr_key, curr_group 467 if curr_key == target_key: 468 for _ in curr_group: 469 pass 470 471 472.. function:: islice(iterable, stop) 473 islice(iterable, start, stop[, step]) 474 475 Make an iterator that returns selected elements from the iterable. 476 Works like sequence slicing but does not support negative values for 477 *start*, *stop*, or *step*. 478 479 If *start* is zero or ``None``, iteration starts at zero. Otherwise, 480 elements from the iterable are skipped until *start* is reached. 481 482 If *stop* is ``None``, iteration continues until the input is 483 exhausted, if at all. Otherwise, it stops at the specified position. 484 485 If *step* is ``None``, the step defaults to one. Elements are returned 486 consecutively unless *step* is set higher than one which results in 487 items being skipped. 488 489 Roughly equivalent to:: 490 491 def islice(iterable, *args): 492 # islice('ABCDEFG', 2) → A B 493 # islice('ABCDEFG', 2, 4) → C D 494 # islice('ABCDEFG', 2, None) → C D E F G 495 # islice('ABCDEFG', 0, None, 2) → A C E G 496 497 s = slice(*args) 498 start = 0 if s.start is None else s.start 499 stop = s.stop 500 step = 1 if s.step is None else s.step 501 if start < 0 or (stop is not None and stop < 0) or step <= 0: 502 raise ValueError 503 504 indices = count() if stop is None else range(max(start, stop)) 505 next_i = start 506 for i, element in zip(indices, iterable): 507 if i == next_i: 508 yield element 509 next_i += step 510 511 If the input is an iterator, then fully consuming the *islice* 512 advances the input iterator by ``max(start, stop)`` steps regardless 513 of the *step* value. 514 515 516.. function:: pairwise(iterable) 517 518 Return successive overlapping pairs taken from the input *iterable*. 519 520 The number of 2-tuples in the output iterator will be one fewer than the 521 number of inputs. It will be empty if the input iterable has fewer than 522 two values. 523 524 Roughly equivalent to:: 525 526 def pairwise(iterable): 527 # pairwise('ABCDEFG') → AB BC CD DE EF FG 528 529 iterator = iter(iterable) 530 a = next(iterator, None) 531 532 for b in iterator: 533 yield a, b 534 a = b 535 536 .. versionadded:: 3.10 537 538 539.. function:: permutations(iterable, r=None) 540 541 Return successive *r* length `permutations of elements 542 <https://www.britannica.com/science/permutation>`_ from the *iterable*. 543 544 If *r* is not specified or is ``None``, then *r* defaults to the length 545 of the *iterable* and all possible full-length permutations 546 are generated. 547 548 The output is a subsequence of :func:`product` where entries with 549 repeated elements have been filtered out. The length of the output is 550 given by :func:`math.perm` which computes ``n! / (n - r)!`` when 551 ``0 ≤ r ≤ n`` or zero when ``r > n``. 552 553 The permutation tuples are emitted in lexicographic order according to 554 the order of the input *iterable*. If the input *iterable* is sorted, 555 the output tuples will be produced in sorted order. 556 557 Elements are treated as unique based on their position, not on their 558 value. If the input elements are unique, there will be no repeated 559 values within a permutation. 560 561 Roughly equivalent to:: 562 563 def permutations(iterable, r=None): 564 # permutations('ABCD', 2) → AB AC AD BA BC BD CA CB CD DA DB DC 565 # permutations(range(3)) → 012 021 102 120 201 210 566 567 pool = tuple(iterable) 568 n = len(pool) 569 r = n if r is None else r 570 if r > n: 571 return 572 573 indices = list(range(n)) 574 cycles = list(range(n, n-r, -1)) 575 yield tuple(pool[i] for i in indices[:r]) 576 577 while n: 578 for i in reversed(range(r)): 579 cycles[i] -= 1 580 if cycles[i] == 0: 581 indices[i:] = indices[i+1:] + indices[i:i+1] 582 cycles[i] = n - i 583 else: 584 j = cycles[i] 585 indices[i], indices[-j] = indices[-j], indices[i] 586 yield tuple(pool[i] for i in indices[:r]) 587 break 588 else: 589 return 590 591 592.. function:: product(*iterables, repeat=1) 593 594 `Cartesian product <https://en.wikipedia.org/wiki/Cartesian_product>`_ 595 of the input iterables. 596 597 Roughly equivalent to nested for-loops in a generator expression. For example, 598 ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``. 599 600 The nested loops cycle like an odometer with the rightmost element advancing 601 on every iteration. This pattern creates a lexicographic ordering so that if 602 the input's iterables are sorted, the product tuples are emitted in sorted 603 order. 604 605 To compute the product of an iterable with itself, specify the number of 606 repetitions with the optional *repeat* keyword argument. For example, 607 ``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``. 608 609 This function is roughly equivalent to the following code, except that the 610 actual implementation does not build up intermediate results in memory:: 611 612 def product(*iterables, repeat=1): 613 # product('ABCD', 'xy') → Ax Ay Bx By Cx Cy Dx Dy 614 # product(range(2), repeat=3) → 000 001 010 011 100 101 110 111 615 616 if repeat < 0: 617 raise ValueError('repeat argument cannot be negative') 618 pools = [tuple(pool) for pool in iterables] * repeat 619 620 result = [[]] 621 for pool in pools: 622 result = [x+[y] for x in result for y in pool] 623 624 for prod in result: 625 yield tuple(prod) 626 627 Before :func:`product` runs, it completely consumes the input iterables, 628 keeping pools of values in memory to generate the products. Accordingly, 629 it is only useful with finite inputs. 630 631 632.. function:: repeat(object[, times]) 633 634 Make an iterator that returns *object* over and over again. Runs indefinitely 635 unless the *times* argument is specified. 636 637 Roughly equivalent to:: 638 639 def repeat(object, times=None): 640 # repeat(10, 3) → 10 10 10 641 if times is None: 642 while True: 643 yield object 644 else: 645 for i in range(times): 646 yield object 647 648 A common use for *repeat* is to supply a stream of constant values to *map* 649 or *zip*: 650 651 .. doctest:: 652 653 >>> list(map(pow, range(10), repeat(2))) 654 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 655 656 657.. function:: starmap(function, iterable) 658 659 Make an iterator that computes the *function* using arguments obtained 660 from the *iterable*. Used instead of :func:`map` when argument 661 parameters have already been "pre-zipped" into tuples. 662 663 The difference between :func:`map` and :func:`starmap` parallels the 664 distinction between ``function(a,b)`` and ``function(*c)``. Roughly 665 equivalent to:: 666 667 def starmap(function, iterable): 668 # starmap(pow, [(2,5), (3,2), (10,3)]) → 32 9 1000 669 for args in iterable: 670 yield function(*args) 671 672 673.. function:: takewhile(predicate, iterable) 674 675 Make an iterator that returns elements from the *iterable* as long as 676 the *predicate* is true. Roughly equivalent to:: 677 678 def takewhile(predicate, iterable): 679 # takewhile(lambda x: x<5, [1,4,6,3,8]) → 1 4 680 for x in iterable: 681 if not predicate(x): 682 break 683 yield x 684 685 Note, the element that first fails the predicate condition is 686 consumed from the input iterator and there is no way to access it. 687 This could be an issue if an application wants to further consume the 688 input iterator after *takewhile* has been run to exhaustion. To work 689 around this problem, consider using `more-iterools before_and_after() 690 <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.before_and_after>`_ 691 instead. 692 693 694.. function:: tee(iterable, n=2) 695 696 Return *n* independent iterators from a single iterable. 697 698 Roughly equivalent to:: 699 700 def tee(iterable, n=2): 701 if n < 0: 702 raise ValueError 703 if n == 0: 704 return () 705 iterator = _tee(iterable) 706 result = [iterator] 707 for _ in range(n - 1): 708 result.append(_tee(iterator)) 709 return tuple(result) 710 711 class _tee: 712 713 def __init__(self, iterable): 714 it = iter(iterable) 715 if isinstance(it, _tee): 716 self.iterator = it.iterator 717 self.link = it.link 718 else: 719 self.iterator = it 720 self.link = [None, None] 721 722 def __iter__(self): 723 return self 724 725 def __next__(self): 726 link = self.link 727 if link[1] is None: 728 link[0] = next(self.iterator) 729 link[1] = [None, None] 730 value, self.link = link 731 return value 732 733 When the input *iterable* is already a tee iterator object, all 734 members of the return tuple are constructed as if they had been 735 produced by the upstream :func:`tee` call. This "flattening step" 736 allows nested :func:`tee` calls to share the same underlying data 737 chain and to have a single update step rather than a chain of calls. 738 739 The flattening property makes tee iterators efficiently peekable: 740 741 .. testcode:: 742 743 def lookahead(tee_iterator): 744 "Return the next value without moving the input forward" 745 [forked_iterator] = tee(tee_iterator, 1) 746 return next(forked_iterator) 747 748 .. doctest:: 749 750 >>> iterator = iter('abcdef') 751 >>> [iterator] = tee(iterator, 1) # Make the input peekable 752 >>> next(iterator) # Move the iterator forward 753 'a' 754 >>> lookahead(iterator) # Check next value 755 'b' 756 >>> next(iterator) # Continue moving forward 757 'b' 758 759 ``tee`` iterators are not threadsafe. A :exc:`RuntimeError` may be 760 raised when simultaneously using iterators returned by the same :func:`tee` 761 call, even if the original *iterable* is threadsafe. 762 763 This itertool may require significant auxiliary storage (depending on how 764 much temporary data needs to be stored). In general, if one iterator uses 765 most or all of the data before another iterator starts, it is faster to use 766 :func:`list` instead of :func:`tee`. 767 768 769.. function:: zip_longest(*iterables, fillvalue=None) 770 771 Make an iterator that aggregates elements from each of the 772 *iterables*. 773 774 If the iterables are of uneven length, missing values are filled-in 775 with *fillvalue*. If not specified, *fillvalue* defaults to ``None``. 776 777 Iteration continues until the longest iterable is exhausted. 778 779 Roughly equivalent to:: 780 781 def zip_longest(*iterables, fillvalue=None): 782 # zip_longest('ABCD', 'xy', fillvalue='-') → Ax By C- D- 783 784 iterators = list(map(iter, iterables)) 785 num_active = len(iterators) 786 if not num_active: 787 return 788 789 while True: 790 values = [] 791 for i, iterator in enumerate(iterators): 792 try: 793 value = next(iterator) 794 except StopIteration: 795 num_active -= 1 796 if not num_active: 797 return 798 iterators[i] = repeat(fillvalue) 799 value = fillvalue 800 values.append(value) 801 yield tuple(values) 802 803 If one of the iterables is potentially infinite, then the :func:`zip_longest` 804 function should be wrapped with something that limits the number of calls 805 (for example :func:`islice` or :func:`takewhile`). 806 807 808.. _itertools-recipes: 809 810Itertools Recipes 811----------------- 812 813This section shows recipes for creating an extended toolset using the existing 814itertools as building blocks. 815 816The primary purpose of the itertools recipes is educational. The recipes show 817various ways of thinking about individual tools — for example, that 818``chain.from_iterable`` is related to the concept of flattening. The recipes 819also give ideas about ways that the tools can be combined — for example, how 820``starmap()`` and ``repeat()`` can work together. The recipes also show patterns 821for using itertools with the :mod:`operator` and :mod:`collections` modules as 822well as with the built-in itertools such as ``map()``, ``filter()``, 823``reversed()``, and ``enumerate()``. 824 825A secondary purpose of the recipes is to serve as an incubator. The 826``accumulate()``, ``compress()``, and ``pairwise()`` itertools started out as 827recipes. Currently, the ``sliding_window()``, ``iter_index()``, and ``sieve()`` 828recipes are being tested to see whether they prove their worth. 829 830Substantially all of these recipes and many, many others can be installed from 831the :pypi:`more-itertools` project found 832on the Python Package Index:: 833 834 python -m pip install more-itertools 835 836Many of the recipes offer the same high performance as the underlying toolset. 837Superior memory performance is kept by processing elements one at a time rather 838than bringing the whole iterable into memory all at once. Code volume is kept 839small by linking the tools together in a `functional style 840<https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`_. High speed 841is retained by preferring "vectorized" building blocks over the use of for-loops 842and :term:`generators <generator>` which incur interpreter overhead. 843 844.. testcode:: 845 846 import collections 847 import contextlib 848 import functools 849 import math 850 import operator 851 import random 852 853 def take(n, iterable): 854 "Return first n items of the iterable as a list." 855 return list(islice(iterable, n)) 856 857 def prepend(value, iterable): 858 "Prepend a single value in front of an iterable." 859 # prepend(1, [2, 3, 4]) → 1 2 3 4 860 return chain([value], iterable) 861 862 def tabulate(function, start=0): 863 "Return function(0), function(1), ..." 864 return map(function, count(start)) 865 866 def repeatfunc(func, times=None, *args): 867 "Repeat calls to func with specified arguments." 868 if times is None: 869 return starmap(func, repeat(args)) 870 return starmap(func, repeat(args, times)) 871 872 def flatten(list_of_lists): 873 "Flatten one level of nesting." 874 return chain.from_iterable(list_of_lists) 875 876 def ncycles(iterable, n): 877 "Returns the sequence elements n times." 878 return chain.from_iterable(repeat(tuple(iterable), n)) 879 880 def tail(n, iterable): 881 "Return an iterator over the last n items." 882 # tail(3, 'ABCDEFG') → E F G 883 return iter(collections.deque(iterable, maxlen=n)) 884 885 def consume(iterator, n=None): 886 "Advance the iterator n-steps ahead. If n is None, consume entirely." 887 # Use functions that consume iterators at C speed. 888 if n is None: 889 collections.deque(iterator, maxlen=0) 890 else: 891 next(islice(iterator, n, n), None) 892 893 def nth(iterable, n, default=None): 894 "Returns the nth item or a default value." 895 return next(islice(iterable, n, None), default) 896 897 def quantify(iterable, predicate=bool): 898 "Given a predicate that returns True or False, count the True results." 899 return sum(map(predicate, iterable)) 900 901 def first_true(iterable, default=False, predicate=None): 902 "Returns the first true value or the *default* if there is no true value." 903 # first_true([a,b,c], x) → a or b or c or x 904 # first_true([a,b], x, f) → a if f(a) else b if f(b) else x 905 return next(filter(predicate, iterable), default) 906 907 def all_equal(iterable, key=None): 908 "Returns True if all the elements are equal to each other." 909 # all_equal('4٤௪౪໔', key=int) → True 910 return len(take(2, groupby(iterable, key))) <= 1 911 912 def unique_justseen(iterable, key=None): 913 "Yield unique elements, preserving order. Remember only the element just seen." 914 # unique_justseen('AAAABBBCCDAABBB') → A B C D A B 915 # unique_justseen('ABBcCAD', str.casefold) → A B c A D 916 if key is None: 917 return map(operator.itemgetter(0), groupby(iterable)) 918 return map(next, map(operator.itemgetter(1), groupby(iterable, key))) 919 920 def unique_everseen(iterable, key=None): 921 "Yield unique elements, preserving order. Remember all elements ever seen." 922 # unique_everseen('AAAABBBCCDAABBB') → A B C D 923 # unique_everseen('ABBcCAD', str.casefold) → A B c D 924 seen = set() 925 if key is None: 926 for element in filterfalse(seen.__contains__, iterable): 927 seen.add(element) 928 yield element 929 else: 930 for element in iterable: 931 k = key(element) 932 if k not in seen: 933 seen.add(k) 934 yield element 935 936 def unique(iterable, key=None, reverse=False): 937 "Yield unique elements in sorted order. Supports unhashable inputs." 938 # unique([[1, 2], [3, 4], [1, 2]]) → [1, 2] [3, 4] 939 return unique_justseen(sorted(iterable, key=key, reverse=reverse), key=key) 940 941 def sliding_window(iterable, n): 942 "Collect data into overlapping fixed-length chunks or blocks." 943 # sliding_window('ABCDEFG', 4) → ABCD BCDE CDEF DEFG 944 iterator = iter(iterable) 945 window = collections.deque(islice(iterator, n - 1), maxlen=n) 946 for x in iterator: 947 window.append(x) 948 yield tuple(window) 949 950 def grouper(iterable, n, *, incomplete='fill', fillvalue=None): 951 "Collect data into non-overlapping fixed-length chunks or blocks." 952 # grouper('ABCDEFG', 3, fillvalue='x') → ABC DEF Gxx 953 # grouper('ABCDEFG', 3, incomplete='strict') → ABC DEF ValueError 954 # grouper('ABCDEFG', 3, incomplete='ignore') → ABC DEF 955 iterators = [iter(iterable)] * n 956 match incomplete: 957 case 'fill': 958 return zip_longest(*iterators, fillvalue=fillvalue) 959 case 'strict': 960 return zip(*iterators, strict=True) 961 case 'ignore': 962 return zip(*iterators) 963 case _: 964 raise ValueError('Expected fill, strict, or ignore') 965 966 def roundrobin(*iterables): 967 "Visit input iterables in a cycle until each is exhausted." 968 # roundrobin('ABC', 'D', 'EF') → A D E B F C 969 # Algorithm credited to George Sakkis 970 iterators = map(iter, iterables) 971 for num_active in range(len(iterables), 0, -1): 972 iterators = cycle(islice(iterators, num_active)) 973 yield from map(next, iterators) 974 975 def subslices(seq): 976 "Return all contiguous non-empty subslices of a sequence." 977 # subslices('ABCD') → A AB ABC ABCD B BC BCD C CD D 978 slices = starmap(slice, combinations(range(len(seq) + 1), 2)) 979 return map(operator.getitem, repeat(seq), slices) 980 981 def iter_index(iterable, value, start=0, stop=None): 982 "Return indices where a value occurs in a sequence or iterable." 983 # iter_index('AABCADEAF', 'A') → 0 1 4 7 984 seq_index = getattr(iterable, 'index', None) 985 if seq_index is None: 986 iterator = islice(iterable, start, stop) 987 for i, element in enumerate(iterator, start): 988 if element is value or element == value: 989 yield i 990 else: 991 stop = len(iterable) if stop is None else stop 992 i = start 993 with contextlib.suppress(ValueError): 994 while True: 995 yield (i := seq_index(value, i, stop)) 996 i += 1 997 998 def iter_except(func, exception, first=None): 999 "Convert a call-until-exception interface to an iterator interface." 1000 # iter_except(d.popitem, KeyError) → non-blocking dictionary iterator 1001 with contextlib.suppress(exception): 1002 if first is not None: 1003 yield first() 1004 while True: 1005 yield func() 1006 1007 1008The following recipes have a more mathematical flavor: 1009 1010.. testcode:: 1011 1012 def powerset(iterable): 1013 "powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" 1014 s = list(iterable) 1015 return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) 1016 1017 def sum_of_squares(iterable): 1018 "Add up the squares of the input values." 1019 # sum_of_squares([10, 20, 30]) → 1400 1020 return math.sumprod(*tee(iterable)) 1021 1022 def reshape(matrix, cols): 1023 "Reshape a 2-D matrix to have a given number of columns." 1024 # reshape([(0, 1), (2, 3), (4, 5)], 3) → (0, 1, 2), (3, 4, 5) 1025 return batched(chain.from_iterable(matrix), cols, strict=True) 1026 1027 def transpose(matrix): 1028 "Swap the rows and columns of a 2-D matrix." 1029 # transpose([(1, 2, 3), (11, 22, 33)]) → (1, 11) (2, 22) (3, 33) 1030 return zip(*matrix, strict=True) 1031 1032 def matmul(m1, m2): 1033 "Multiply two matrices." 1034 # matmul([(7, 5), (3, 5)], [(2, 5), (7, 9)]) → (49, 80), (41, 60) 1035 n = len(m2[0]) 1036 return batched(starmap(math.sumprod, product(m1, transpose(m2))), n) 1037 1038 def convolve(signal, kernel): 1039 """Discrete linear convolution of two iterables. 1040 Equivalent to polynomial multiplication. 1041 1042 Convolutions are mathematically commutative; however, the inputs are 1043 evaluated differently. The signal is consumed lazily and can be 1044 infinite. The kernel is fully consumed before the calculations begin. 1045 1046 Article: https://betterexplained.com/articles/intuitive-convolution/ 1047 Video: https://www.youtube.com/watch?v=KuXjwB4LzSA 1048 """ 1049 # convolve([1, -1, -20], [1, -3]) → 1 -4 -17 60 1050 # convolve(data, [0.25, 0.25, 0.25, 0.25]) → Moving average (blur) 1051 # convolve(data, [1/2, 0, -1/2]) → 1st derivative estimate 1052 # convolve(data, [1, -2, 1]) → 2nd derivative estimate 1053 kernel = tuple(kernel)[::-1] 1054 n = len(kernel) 1055 padded_signal = chain(repeat(0, n-1), signal, repeat(0, n-1)) 1056 windowed_signal = sliding_window(padded_signal, n) 1057 return map(math.sumprod, repeat(kernel), windowed_signal) 1058 1059 def polynomial_from_roots(roots): 1060 """Compute a polynomial's coefficients from its roots. 1061 1062 (x - 5) (x + 4) (x - 3) expands to: x³ -4x² -17x + 60 1063 """ 1064 # polynomial_from_roots([5, -4, 3]) → [1, -4, -17, 60] 1065 factors = zip(repeat(1), map(operator.neg, roots)) 1066 return list(functools.reduce(convolve, factors, [1])) 1067 1068 def polynomial_eval(coefficients, x): 1069 """Evaluate a polynomial at a specific value. 1070 1071 Computes with better numeric stability than Horner's method. 1072 """ 1073 # Evaluate x³ -4x² -17x + 60 at x = 5 1074 # polynomial_eval([1, -4, -17, 60], x=5) → 0 1075 n = len(coefficients) 1076 if not n: 1077 return type(x)(0) 1078 powers = map(pow, repeat(x), reversed(range(n))) 1079 return math.sumprod(coefficients, powers) 1080 1081 def polynomial_derivative(coefficients): 1082 """Compute the first derivative of a polynomial. 1083 1084 f(x) = x³ -4x² -17x + 60 1085 f'(x) = 3x² -8x -17 1086 """ 1087 # polynomial_derivative([1, -4, -17, 60]) → [3, -8, -17] 1088 n = len(coefficients) 1089 powers = reversed(range(1, n)) 1090 return list(map(operator.mul, coefficients, powers)) 1091 1092 def sieve(n): 1093 "Primes less than n." 1094 # sieve(30) → 2 3 5 7 11 13 17 19 23 29 1095 if n > 2: 1096 yield 2 1097 data = bytearray((0, 1)) * (n // 2) 1098 for p in iter_index(data, 1, start=3, stop=math.isqrt(n) + 1): 1099 data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p))) 1100 yield from iter_index(data, 1, start=3) 1101 1102 def factor(n): 1103 "Prime factors of n." 1104 # factor(99) → 3 3 11 1105 # factor(1_000_000_000_000_007) → 47 59 360620266859 1106 # factor(1_000_000_000_000_403) → 1000000000000403 1107 for prime in sieve(math.isqrt(n) + 1): 1108 while not n % prime: 1109 yield prime 1110 n //= prime 1111 if n == 1: 1112 return 1113 if n > 1: 1114 yield n 1115 1116 def totient(n): 1117 "Count of natural numbers up to n that are coprime to n." 1118 # https://mathworld.wolfram.com/TotientFunction.html 1119 # totient(12) → 4 because len([1, 5, 7, 11]) == 4 1120 for prime in set(factor(n)): 1121 n -= n // prime 1122 return n 1123 1124 1125.. doctest:: 1126 :hide: 1127 1128 These examples no longer appear in the docs but are guaranteed 1129 to keep working. 1130 1131 >>> amounts = [120.15, 764.05, 823.14] 1132 >>> for checknum, amount in zip(count(1200), amounts): 1133 ... print('Check %d is for $%.2f' % (checknum, amount)) 1134 ... 1135 Check 1200 is for $120.15 1136 Check 1201 is for $764.05 1137 Check 1202 is for $823.14 1138 1139 >>> import operator 1140 >>> for cube in map(operator.pow, range(1,4), repeat(3)): 1141 ... print(cube) 1142 ... 1143 1 1144 8 1145 27 1146 1147 >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele'] 1148 >>> for name in islice(reportlines, 3, None, 2): 1149 ... print(name.title()) 1150 ... 1151 Alex 1152 Laura 1153 Martin 1154 Walter 1155 Samuele 1156 1157 >>> from operator import itemgetter 1158 >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) 1159 >>> di = sorted(sorted(d.items()), key=itemgetter(1)) 1160 >>> for k, g in groupby(di, itemgetter(1)): 1161 ... print(k, list(map(itemgetter(0), g))) 1162 ... 1163 1 ['a', 'c', 'e'] 1164 2 ['b', 'd', 'f'] 1165 3 ['g'] 1166 1167 # Find runs of consecutive numbers using groupby. The key to the solution 1168 # is differencing with a range so that consecutive numbers all appear in 1169 # same group. 1170 >>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28] 1171 >>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]): 1172 ... print(list(map(operator.itemgetter(1), g))) 1173 ... 1174 [1] 1175 [4, 5, 6] 1176 [10] 1177 [15, 16, 17, 18] 1178 [22] 1179 [25, 26, 27, 28] 1180 1181 Now, we test all of the itertool recipes 1182 1183 >>> take(10, count()) 1184 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 1185 >>> # Verify that the input is consumed lazily 1186 >>> it = iter('abcdef') 1187 >>> take(3, it) 1188 ['a', 'b', 'c'] 1189 >>> list(it) 1190 ['d', 'e', 'f'] 1191 1192 1193 >>> list(prepend(1, [2, 3, 4])) 1194 [1, 2, 3, 4] 1195 1196 1197 >>> list(enumerate('abc')) 1198 [(0, 'a'), (1, 'b'), (2, 'c')] 1199 1200 1201 >>> list(islice(tabulate(lambda x: 2*x), 4)) 1202 [0, 2, 4, 6] 1203 1204 1205 >>> list(tail(3, 'ABCDEFG')) 1206 ['E', 'F', 'G'] 1207 >>> # Verify the input is consumed greedily 1208 >>> input_iterator = iter('ABCDEFG') 1209 >>> output_iterator = tail(3, input_iterator) 1210 >>> list(input_iterator) 1211 [] 1212 1213 1214 >>> it = iter(range(10)) 1215 >>> consume(it, 3) 1216 >>> # Verify the input is consumed lazily 1217 >>> next(it) 1218 3 1219 >>> # Verify the input is consumed completely 1220 >>> consume(it) 1221 >>> next(it, 'Done') 1222 'Done' 1223 1224 1225 >>> nth('abcde', 3) 1226 'd' 1227 >>> nth('abcde', 9) is None 1228 True 1229 >>> # Verify that the input is consumed lazily 1230 >>> it = iter('abcde') 1231 >>> nth(it, 2) 1232 'c' 1233 >>> list(it) 1234 ['d', 'e'] 1235 1236 1237 >>> [all_equal(s) for s in ('', 'A', 'AAAA', 'AAAB', 'AAABA')] 1238 [True, True, True, False, False] 1239 >>> [all_equal(s, key=str.casefold) for s in ('', 'A', 'AaAa', 'AAAB', 'AAABA')] 1240 [True, True, True, False, False] 1241 >>> # Verify that the input is consumed lazily and that only 1242 >>> # one element of a second equivalence class is used to disprove 1243 >>> # the assertion that all elements are equal. 1244 >>> it = iter('aaabbbccc') 1245 >>> all_equal(it) 1246 False 1247 >>> ''.join(it) 1248 'bbccc' 1249 1250 1251 >>> quantify(range(99), lambda x: x%2==0) 1252 50 1253 >>> quantify([True, False, False, True, True]) 1254 3 1255 >>> quantify(range(12), predicate=lambda x: x%2==1) 1256 6 1257 1258 1259 >>> a = [[1, 2, 3], [4, 5, 6]] 1260 >>> list(flatten(a)) 1261 [1, 2, 3, 4, 5, 6] 1262 1263 1264 >>> list(ncycles('abc', 3)) 1265 ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] 1266 >>> # Verify greedy consumption of input iterator 1267 >>> input_iterator = iter('abc') 1268 >>> output_iterator = ncycles(input_iterator, 3) 1269 >>> list(input_iterator) 1270 [] 1271 1272 1273 >>> sum_of_squares([10, 20, 30]) 1274 1400 1275 1276 1277 >>> list(reshape([(0, 1), (2, 3), (4, 5)], 3)) 1278 [(0, 1, 2), (3, 4, 5)] 1279 >>> M = [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)] 1280 >>> list(reshape(M, 1)) 1281 [(0,), (1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,), (9,), (10,), (11,)] 1282 >>> list(reshape(M, 2)) 1283 [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11)] 1284 >>> list(reshape(M, 3)) 1285 [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)] 1286 >>> list(reshape(M, 4)) 1287 [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)] 1288 >>> list(reshape(M, 5)) 1289 Traceback (most recent call last): 1290 ... 1291 ValueError: batched(): incomplete batch 1292 >>> list(reshape(M, 6)) 1293 [(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10, 11)] 1294 >>> list(reshape(M, 12)) 1295 [(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)] 1296 1297 1298 >>> list(transpose([(1, 2, 3), (11, 22, 33)])) 1299 [(1, 11), (2, 22), (3, 33)] 1300 >>> # Verify that the inputs are consumed lazily 1301 >>> input1 = iter([1, 2, 3]) 1302 >>> input2 = iter([11, 22, 33]) 1303 >>> output_iterator = transpose([input1, input2]) 1304 >>> next(output_iterator) 1305 (1, 11) 1306 >>> list(zip(input1, input2)) 1307 [(2, 22), (3, 33)] 1308 1309 1310 >>> list(matmul([(7, 5), (3, 5)], [[2, 5], [7, 9]])) 1311 [(49, 80), (41, 60)] 1312 >>> list(matmul([[2, 5], [7, 9], [3, 4]], [[7, 11, 5, 4, 9], [3, 5, 2, 6, 3]])) 1313 [(29, 47, 20, 38, 33), (76, 122, 53, 82, 90), (33, 53, 23, 36, 39)] 1314 1315 1316 >>> list(convolve([1, -1, -20], [1, -3])) == [1, -4, -17, 60] 1317 True 1318 >>> data = [20, 40, 24, 32, 20, 28, 16] 1319 >>> list(convolve(data, [0.25, 0.25, 0.25, 0.25])) 1320 [5.0, 15.0, 21.0, 29.0, 29.0, 26.0, 24.0, 16.0, 11.0, 4.0] 1321 >>> list(convolve(data, [1, -1])) 1322 [20, 20, -16, 8, -12, 8, -12, -16] 1323 >>> list(convolve(data, [1, -2, 1])) 1324 [20, 0, -36, 24, -20, 20, -20, -4, 16] 1325 >>> # Verify signal is consumed lazily and the kernel greedily 1326 >>> signal_iterator = iter([10, 20, 30, 40, 50]) 1327 >>> kernel_iterator = iter([1, 2, 3]) 1328 >>> output_iterator = convolve(signal_iterator, kernel_iterator) 1329 >>> list(kernel_iterator) 1330 [] 1331 >>> next(output_iterator) 1332 10 1333 >>> next(output_iterator) 1334 40 1335 >>> list(signal_iterator) 1336 [30, 40, 50] 1337 1338 1339 >>> from fractions import Fraction 1340 >>> from decimal import Decimal 1341 >>> polynomial_eval([1, -4, -17, 60], x=5) 1342 0 1343 >>> x = 5; x**3 - 4*x**2 -17*x + 60 1344 0 1345 >>> polynomial_eval([1, -4, -17, 60], x=2.5) 1346 8.125 1347 >>> x = 2.5; x**3 - 4*x**2 -17*x + 60 1348 8.125 1349 >>> polynomial_eval([1, -4, -17, 60], x=Fraction(2, 3)) 1350 Fraction(1274, 27) 1351 >>> x = Fraction(2, 3); x**3 - 4*x**2 -17*x + 60 1352 Fraction(1274, 27) 1353 >>> polynomial_eval([1, -4, -17, 60], x=Decimal('1.75')) 1354 Decimal('23.359375') 1355 >>> x = Decimal('1.75'); x**3 - 4*x**2 -17*x + 60 1356 Decimal('23.359375') 1357 >>> polynomial_eval([], 2) 1358 0 1359 >>> polynomial_eval([], 2.5) 1360 0.0 1361 >>> polynomial_eval([], Fraction(2, 3)) 1362 Fraction(0, 1) 1363 >>> polynomial_eval([], Decimal('1.75')) 1364 Decimal('0') 1365 >>> polynomial_eval([11], 7) == 11 1366 True 1367 >>> polynomial_eval([11, 2], 7) == 11 * 7 + 2 1368 True 1369 1370 1371 >>> polynomial_from_roots([5, -4, 3]) 1372 [1, -4, -17, 60] 1373 >>> factored = lambda x: (x - 5) * (x + 4) * (x - 3) 1374 >>> expanded = lambda x: x**3 -4*x**2 -17*x + 60 1375 >>> all(factored(x) == expanded(x) for x in range(-10, 11)) 1376 True 1377 1378 1379 >>> polynomial_derivative([1, -4, -17, 60]) 1380 [3, -8, -17] 1381 1382 1383 >>> list(iter_index('AABCADEAF', 'A')) 1384 [0, 1, 4, 7] 1385 >>> list(iter_index('AABCADEAF', 'B')) 1386 [2] 1387 >>> list(iter_index('AABCADEAF', 'X')) 1388 [] 1389 >>> list(iter_index('', 'X')) 1390 [] 1391 >>> list(iter_index('AABCADEAF', 'A', 1)) 1392 [1, 4, 7] 1393 >>> list(iter_index(iter('AABCADEAF'), 'A', 1)) 1394 [1, 4, 7] 1395 >>> list(iter_index('AABCADEAF', 'A', 2)) 1396 [4, 7] 1397 >>> list(iter_index(iter('AABCADEAF'), 'A', 2)) 1398 [4, 7] 1399 >>> list(iter_index('AABCADEAF', 'A', 10)) 1400 [] 1401 >>> list(iter_index(iter('AABCADEAF'), 'A', 10)) 1402 [] 1403 >>> list(iter_index('AABCADEAF', 'A', 1, 7)) 1404 [1, 4] 1405 >>> list(iter_index(iter('AABCADEAF'), 'A', 1, 7)) 1406 [1, 4] 1407 >>> # Verify that ValueErrors not swallowed (gh-107208) 1408 >>> def assert_no_value(iterable, forbidden_value): 1409 ... for item in iterable: 1410 ... if item == forbidden_value: 1411 ... raise ValueError 1412 ... yield item 1413 ... 1414 >>> list(iter_index(assert_no_value('AABCADEAF', 'B'), 'A')) 1415 Traceback (most recent call last): 1416 ... 1417 ValueError 1418 >>> # Verify that both paths can find identical NaN values 1419 >>> x = float('NaN') 1420 >>> y = float('NaN') 1421 >>> list(iter_index([0, x, x, y, 0], x)) 1422 [1, 2] 1423 >>> list(iter_index(iter([0, x, x, y, 0]), x)) 1424 [1, 2] 1425 >>> # Test list input. Lists do not support None for the stop argument 1426 >>> list(iter_index(list('AABCADEAF'), 'A')) 1427 [0, 1, 4, 7] 1428 >>> # Verify that input is consumed lazily 1429 >>> input_iterator = iter('AABCADEAF') 1430 >>> output_iterator = iter_index(input_iterator, 'A') 1431 >>> next(output_iterator) 1432 0 1433 >>> next(output_iterator) 1434 1 1435 >>> next(output_iterator) 1436 4 1437 >>> ''.join(input_iterator) 1438 'DEAF' 1439 1440 1441 >>> # Verify that the target value can be a sequence. 1442 >>> seq = [[10, 20], [30, 40], 30, 40, [30, 40], 50] 1443 >>> target = [30, 40] 1444 >>> list(iter_index(seq, target)) 1445 [1, 4] 1446 1447 1448 >>> # Verify faithfulness to type specific index() method behaviors. 1449 >>> # For example, bytes and str perform continuous-subsequence searches 1450 >>> # that do not match the general behavior specified 1451 >>> # in collections.abc.Sequence.index(). 1452 >>> seq = 'abracadabra' 1453 >>> target = 'ab' 1454 >>> list(iter_index(seq, target)) 1455 [0, 7] 1456 1457 1458 >>> list(sieve(30)) 1459 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] 1460 >>> small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] 1461 >>> all(list(sieve(n)) == [p for p in small_primes if p < n] for n in range(101)) 1462 True 1463 >>> len(list(sieve(100))) 1464 25 1465 >>> len(list(sieve(1_000))) 1466 168 1467 >>> len(list(sieve(10_000))) 1468 1229 1469 >>> len(list(sieve(100_000))) 1470 9592 1471 >>> len(list(sieve(1_000_000))) 1472 78498 1473 >>> carmichael = {561, 1105, 1729, 2465, 2821, 6601, 8911} # https://oeis.org/A002997 1474 >>> set(sieve(10_000)).isdisjoint(carmichael) 1475 True 1476 1477 1478 >>> list(factor(99)) # Code example 1 1479 [3, 3, 11] 1480 >>> list(factor(1_000_000_000_000_007)) # Code example 2 1481 [47, 59, 360620266859] 1482 >>> list(factor(1_000_000_000_000_403)) # Code example 3 1483 [1000000000000403] 1484 >>> list(factor(0)) 1485 [] 1486 >>> list(factor(1)) 1487 [] 1488 >>> list(factor(2)) 1489 [2] 1490 >>> list(factor(3)) 1491 [3] 1492 >>> list(factor(4)) 1493 [2, 2] 1494 >>> list(factor(5)) 1495 [5] 1496 >>> list(factor(6)) 1497 [2, 3] 1498 >>> list(factor(7)) 1499 [7] 1500 >>> list(factor(8)) 1501 [2, 2, 2] 1502 >>> list(factor(9)) 1503 [3, 3] 1504 >>> list(factor(10)) 1505 [2, 5] 1506 >>> list(factor(128_884_753_939)) # large prime 1507 [128884753939] 1508 >>> list(factor(999953 * 999983)) # large semiprime 1509 [999953, 999983] 1510 >>> list(factor(6 ** 20)) == [2] * 20 + [3] * 20 # large power 1511 True 1512 >>> list(factor(909_909_090_909)) # large multiterm composite 1513 [3, 3, 7, 13, 13, 751, 113797] 1514 >>> math.prod([3, 3, 7, 13, 13, 751, 113797]) 1515 909909090909 1516 >>> all(math.prod(factor(n)) == n for n in range(1, 2_000)) 1517 True 1518 >>> all(set(factor(n)) <= set(sieve(n+1)) for n in range(2_000)) 1519 True 1520 >>> all(list(factor(n)) == sorted(factor(n)) for n in range(2_000)) 1521 True 1522 1523 1524 >>> totient(0) # https://www.wolframalpha.com/input?i=totient+0 1525 0 1526 >>> first_totients = [1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4, 12, 6, 8, 8, 16, 6, 1527 ... 18, 8, 12, 10, 22, 8, 20, 12, 18, 12, 28, 8, 30, 16, 20, 16, 24, 12, 36, 18, 1528 ... 24, 16, 40, 12, 42, 20, 24, 22, 46, 16, 42, 20, 32, 24, 52, 18, 40, 24, 36, 1529 ... 28, 58, 16, 60, 30, 36, 32, 48, 20, 66, 32, 44] # https://oeis.org/A000010 1530 ... 1531 >>> list(map(totient, range(1, 70))) == first_totients 1532 True 1533 >>> reference_totient = lambda n: sum(math.gcd(t, n) == 1 for t in range(1, n+1)) 1534 >>> all(totient(n) == reference_totient(n) for n in range(1000)) 1535 True 1536 >>> totient(128_884_753_939) == 128_884_753_938 # large prime 1537 True 1538 >>> totient(999953 * 999983) == 999952 * 999982 # large semiprime 1539 True 1540 >>> totient(6 ** 20) == 1 * 2**19 * 2 * 3**19 # repeated primes 1541 True 1542 1543 1544 >>> list(flatten([('a', 'b'), (), ('c', 'd', 'e'), ('f',), ('g', 'h', 'i')])) 1545 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] 1546 1547 1548 >>> list(repeatfunc(pow, 5, 2, 3)) 1549 [8, 8, 8, 8, 8] 1550 >>> take(5, map(int, repeatfunc(random.random))) 1551 [0, 0, 0, 0, 0] 1552 >>> random.seed(85753098575309) 1553 >>> list(repeatfunc(random.random, 3)) 1554 [0.16370491282496968, 0.45889608687313455, 0.3747076837820118] 1555 >>> list(repeatfunc(chr, 3, 65)) 1556 ['A', 'A', 'A'] 1557 >>> list(repeatfunc(pow, 3, 2, 5)) 1558 [32, 32, 32] 1559 1560 1561 >>> list(grouper('abcdefg', 3, fillvalue='x')) 1562 [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')] 1563 1564 1565 >>> it = grouper('abcdefg', 3, incomplete='strict') 1566 >>> next(it) 1567 ('a', 'b', 'c') 1568 >>> next(it) 1569 ('d', 'e', 'f') 1570 >>> next(it) 1571 Traceback (most recent call last): 1572 ... 1573 ValueError: zip() argument 2 is shorter than argument 1 1574 1575 >>> list(grouper('abcdefg', n=3, incomplete='ignore')) 1576 [('a', 'b', 'c'), ('d', 'e', 'f')] 1577 1578 1579 >>> list(sliding_window('ABCDEFG', 1)) 1580 [('A',), ('B',), ('C',), ('D',), ('E',), ('F',), ('G',)] 1581 >>> list(sliding_window('ABCDEFG', 2)) 1582 [('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'F'), ('F', 'G')] 1583 >>> list(sliding_window('ABCDEFG', 3)) 1584 [('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')] 1585 >>> list(sliding_window('ABCDEFG', 4)) 1586 [('A', 'B', 'C', 'D'), ('B', 'C', 'D', 'E'), ('C', 'D', 'E', 'F'), ('D', 'E', 'F', 'G')] 1587 >>> list(sliding_window('ABCDEFG', 5)) 1588 [('A', 'B', 'C', 'D', 'E'), ('B', 'C', 'D', 'E', 'F'), ('C', 'D', 'E', 'F', 'G')] 1589 >>> list(sliding_window('ABCDEFG', 6)) 1590 [('A', 'B', 'C', 'D', 'E', 'F'), ('B', 'C', 'D', 'E', 'F', 'G')] 1591 >>> list(sliding_window('ABCDEFG', 7)) 1592 [('A', 'B', 'C', 'D', 'E', 'F', 'G')] 1593 >>> list(sliding_window('ABCDEFG', 8)) 1594 [] 1595 >>> try: 1596 ... list(sliding_window('ABCDEFG', -1)) 1597 ... except ValueError: 1598 ... 'zero or negative n not supported' 1599 ... 1600 'zero or negative n not supported' 1601 >>> try: 1602 ... list(sliding_window('ABCDEFG', 0)) 1603 ... except ValueError: 1604 ... 'zero or negative n not supported' 1605 ... 1606 'zero or negative n not supported' 1607 1608 1609 >>> list(roundrobin('abc', 'd', 'ef')) 1610 ['a', 'd', 'e', 'b', 'f', 'c'] 1611 >>> ranges = [range(5, 1000), range(4, 3000), range(0), range(3, 2000), range(2, 5000), range(1, 3500)] 1612 >>> collections.Counter(roundrobin(*ranges)) == collections.Counter(chain(*ranges)) 1613 True 1614 >>> # Verify that the inputs are consumed lazily 1615 >>> input_iterators = list(map(iter, ['abcd', 'ef', '', 'ghijk', 'l', 'mnopqr'])) 1616 >>> output_iterator = roundrobin(*input_iterators) 1617 >>> ''.join(islice(output_iterator, 10)) 1618 'aeglmbfhnc' 1619 >>> ''.join(chain(*input_iterators)) 1620 'dijkopqr' 1621 1622 1623 >>> list(subslices('ABCD')) 1624 ['A', 'AB', 'ABC', 'ABCD', 'B', 'BC', 'BCD', 'C', 'CD', 'D'] 1625 1626 1627 >>> list(powerset([1,2,3])) 1628 [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)] 1629 >>> all(len(list(powerset(range(n)))) == 2**n for n in range(18)) 1630 True 1631 >>> list(powerset('abcde')) == sorted(sorted(set(powerset('abcde'))), key=len) 1632 True 1633 1634 1635 >>> list(unique_everseen('AAAABBBCCDAABBB')) 1636 ['A', 'B', 'C', 'D'] 1637 >>> list(unique_everseen('ABBCcAD', str.casefold)) 1638 ['A', 'B', 'C', 'D'] 1639 >>> list(unique_everseen('ABBcCAD', str.casefold)) 1640 ['A', 'B', 'c', 'D'] 1641 >>> # Verify that the input is consumed lazily 1642 >>> input_iterator = iter('AAAABBBCCDAABBB') 1643 >>> output_iterator = unique_everseen(input_iterator) 1644 >>> next(output_iterator) 1645 'A' 1646 >>> ''.join(input_iterator) 1647 'AAABBBCCDAABBB' 1648 1649 1650 >>> list(unique_justseen('AAAABBBCCDAABBB')) 1651 ['A', 'B', 'C', 'D', 'A', 'B'] 1652 >>> list(unique_justseen('ABBCcAD', str.casefold)) 1653 ['A', 'B', 'C', 'A', 'D'] 1654 >>> list(unique_justseen('ABBcCAD', str.casefold)) 1655 ['A', 'B', 'c', 'A', 'D'] 1656 >>> # Verify that the input is consumed lazily 1657 >>> input_iterator = iter('AAAABBBCCDAABBB') 1658 >>> output_iterator = unique_justseen(input_iterator) 1659 >>> next(output_iterator) 1660 'A' 1661 >>> ''.join(input_iterator) 1662 'AAABBBCCDAABBB' 1663 1664 1665 >>> list(unique([[1, 2], [3, 4], [1, 2]])) 1666 [[1, 2], [3, 4]] 1667 >>> list(unique('ABBcCAD', str.casefold)) 1668 ['A', 'B', 'c', 'D'] 1669 >>> list(unique('ABBcCAD', str.casefold, reverse=True)) 1670 ['D', 'c', 'B', 'A'] 1671 1672 1673 >>> d = dict(a=1, b=2, c=3) 1674 >>> it = iter_except(d.popitem, KeyError) 1675 >>> d['d'] = 4 1676 >>> next(it) 1677 ('d', 4) 1678 >>> next(it) 1679 ('c', 3) 1680 >>> next(it) 1681 ('b', 2) 1682 >>> d['e'] = 5 1683 >>> next(it) 1684 ('e', 5) 1685 >>> next(it) 1686 ('a', 1) 1687 >>> next(it, 'empty') 1688 'empty' 1689 1690 1691 >>> first_true('ABC0DEF1', '9', str.isdigit) 1692 '0' 1693 >>> # Verify that inputs are consumed lazily 1694 >>> it = iter('ABC0DEF1') 1695 >>> first_true(it, predicate=str.isdigit) 1696 '0' 1697 >>> ''.join(it) 1698 'DEF1' 1699 1700 1701.. testcode:: 1702 :hide: 1703 1704 # Old recipes and their tests which are guaranteed to continue to work. 1705 1706 def sumprod(vec1, vec2): 1707 "Compute a sum of products." 1708 return sum(starmap(operator.mul, zip(vec1, vec2, strict=True))) 1709 1710 def dotproduct(vec1, vec2): 1711 return sum(map(operator.mul, vec1, vec2)) 1712 1713 def pad_none(iterable): 1714 """Returns the sequence elements and then returns None indefinitely. 1715 1716 Useful for emulating the behavior of the built-in map() function. 1717 """ 1718 return chain(iterable, repeat(None)) 1719 1720 def triplewise(iterable): 1721 "Return overlapping triplets from an iterable" 1722 # triplewise('ABCDEFG') → ABC BCD CDE DEF EFG 1723 for (a, _), (b, c) in pairwise(pairwise(iterable)): 1724 yield a, b, c 1725 1726 def nth_combination(iterable, r, index): 1727 "Equivalent to list(combinations(iterable, r))[index]" 1728 pool = tuple(iterable) 1729 n = len(pool) 1730 c = math.comb(n, r) 1731 if index < 0: 1732 index += c 1733 if index < 0 or index >= c: 1734 raise IndexError 1735 result = [] 1736 while r: 1737 c, n, r = c*r//n, n-1, r-1 1738 while index >= c: 1739 index -= c 1740 c, n = c*(n-r)//n, n-1 1741 result.append(pool[-1-n]) 1742 return tuple(result) 1743 1744 def before_and_after(predicate, it): 1745 """ Variant of takewhile() that allows complete 1746 access to the remainder of the iterator. 1747 1748 >>> it = iter('ABCdEfGhI') 1749 >>> all_upper, remainder = before_and_after(str.isupper, it) 1750 >>> ''.join(all_upper) 1751 'ABC' 1752 >>> ''.join(remainder) # takewhile() would lose the 'd' 1753 'dEfGhI' 1754 1755 Note that the true iterator must be fully consumed 1756 before the remainder iterator can generate valid results. 1757 """ 1758 it = iter(it) 1759 transition = [] 1760 1761 def true_iterator(): 1762 for elem in it: 1763 if predicate(elem): 1764 yield elem 1765 else: 1766 transition.append(elem) 1767 return 1768 1769 return true_iterator(), chain(transition, it) 1770 1771 def partition(predicate, iterable): 1772 """Partition entries into false entries and true entries. 1773 1774 If *predicate* is slow, consider wrapping it with functools.lru_cache(). 1775 """ 1776 # partition(is_odd, range(10)) → 0 2 4 6 8 and 1 3 5 7 9 1777 t1, t2 = tee(iterable) 1778 return filterfalse(predicate, t1), filter(predicate, t2) 1779 1780 1781 1782.. doctest:: 1783 :hide: 1784 1785 >>> dotproduct([1,2,3], [4,5,6]) 1786 32 1787 1788 1789 >>> sumprod([1,2,3], [4,5,6]) 1790 32 1791 1792 1793 >>> list(islice(pad_none('abc'), 0, 6)) 1794 ['a', 'b', 'c', None, None, None] 1795 1796 1797 >>> list(triplewise('ABCDEFG')) 1798 [('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')] 1799 1800 1801 >>> population = 'ABCDEFGH' 1802 >>> for r in range(len(population) + 1): 1803 ... seq = list(combinations(population, r)) 1804 ... for i in range(len(seq)): 1805 ... assert nth_combination(population, r, i) == seq[i] 1806 ... for i in range(-len(seq), 0): 1807 ... assert nth_combination(population, r, i) == seq[i] 1808 ... 1809 >>> iterable = 'abcde' 1810 >>> r = 3 1811 >>> combos = list(combinations(iterable, r)) 1812 >>> all(nth_combination(iterable, r, i) == comb for i, comb in enumerate(combos)) 1813 True 1814 1815 1816 >>> it = iter('ABCdEfGhI') 1817 >>> all_upper, remainder = before_and_after(str.isupper, it) 1818 >>> ''.join(all_upper) 1819 'ABC' 1820 >>> ''.join(remainder) 1821 'dEfGhI' 1822 1823 1824 >>> def is_odd(x): 1825 ... return x % 2 == 1 1826 ... 1827 >>> evens, odds = partition(is_odd, range(10)) 1828 >>> list(evens) 1829 [0, 2, 4, 6, 8] 1830 >>> list(odds) 1831 [1, 3, 5, 7, 9] 1832 >>> # Verify that the input is consumed lazily 1833 >>> input_iterator = iter(range(10)) 1834 >>> evens, odds = partition(is_odd, input_iterator) 1835 >>> next(odds) 1836 1 1837 >>> next(odds) 1838 3 1839 >>> next(evens) 1840 0 1841 >>> list(input_iterator) 1842 [4, 5, 6, 7, 8, 9] 1843