[3.12] Misc improvements to the itertools docs (gh-119040) by miss-islington · Pull Request #119044 · python/cpython
def chain(*iterables): # chain('ABC', 'DEF') → A B C D E F for it in iterables: for element in it: yield element for iterable in iterables: yield from iterable
.. classmethod:: chain.from_iterable(iterable)
def from_iterable(iterables): # chain.from_iterable(['ABC', 'DEF']) → A B C D E F for it in iterables: for element in it: yield element for iterable in iterables: yield from iterable
.. function:: combinations(iterable, r)
Return *n* independent iterators from a single iterable.
The following Python code helps explain what *tee* does (although the actual implementation is more complex and uses only a single underlying :abbr:`FIFO (first-in, first-out)` queue):: Roughly equivalent to::
def tee(iterable, n=2): it = iter(iterable) deques = [collections.deque() for i in range(n)] def gen(mydeque): while True: if not mydeque: # when the local deque is empty try: newval = next(it) # fetch a new value and except StopIteration: return for d in deques: # load it to all the deques d.append(newval) yield mydeque.popleft() return tuple(gen(d) for d in deques) iterator = iter(iterable) empty_link = [None, None] # Singly linked list: [value, link] return tuple(_tee(iterator, empty_link) for _ in range(n))
def _tee(iterator, link): while True: if link[1] is None: try: link[:] = [next(iterator), [None, None]] except StopIteration: return value, link = link yield value
Once a :func:`tee` has been created, the original *iterable* should not be used anywhere else; otherwise, the *iterable* could get advanced without
import collections import contextlib import functools import math import operator
def iter_except(func, exception, first=None): "Convert a call-until-exception interface to an iterator interface." # iter_except(d.popitem, KeyError) → non-blocking dictionary iterator try: with contextlib.suppress(exception): if first is not None: yield first() while True: yield func() except exception: pass
The following recipes have a more mathematical flavor: