diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index aba1e25f091..14707214658 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -245,6 +245,20 @@ loops that truncate the stream. for x in iterable: yield x +.. function:: filterfalse(predicate, iterable) + + Make an iterator that filters elements from iterable returning only those for + which the predicate is ``False``. If *predicate* is ``None``, return the items + that are false. Equivalent to:: + + def filterfalse(predicate, iterable): + # filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8 + if predicate is None: + predicate = bool + for x in iterable: + if not predicate(x): + yield x + .. function:: groupby(iterable[, key]) @@ -298,21 +312,6 @@ loops that truncate the stream. self.currkey = self.keyfunc(self.currvalue) -.. function:: filterfalse(predicate, iterable) - - Make an iterator that filters elements from iterable returning only those for - which the predicate is ``False``. If *predicate* is ``None``, return the items - that are false. Equivalent to:: - - def filterfalse(predicate, iterable): - # filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8 - if predicate is None: - predicate = bool - for x in iterable: - if not predicate(x): - yield x - - .. function:: islice(iterable, [start,] stop [, step]) Make an iterator that returns selected elements from the iterable. If *start* is @@ -342,30 +341,6 @@ loops that truncate the stream. then the step defaults to one. -.. function:: zip_longest(*iterables[, fillvalue]) - - Make an iterator that aggregates elements from each of the iterables. If the - iterables are of uneven length, missing values are filled-in with *fillvalue*. - Iteration continues until the longest iterable is exhausted. Equivalent to:: - - def zip_longest(*args, fillvalue=None): - # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- - def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): - yield counter() # yields the fillvalue, or raises IndexError - fillers = repeat(fillvalue) - iters = [chain(it, sentinel(), fillers) for it in args] - try: - for tup in zip(*iters): - yield tup - except IndexError: - pass - - If one of the iterables is potentially infinite, then the :func:`zip_longest` - function should be wrapped with something that limits the number of calls - (for example :func:`islice` or :func:`takewhile`). If not specified, - *fillvalue* defaults to ``None``. - - .. function:: permutations(iterable[, r]) Return successive *r* length permutations of elements in the *iterable*. @@ -525,6 +500,30 @@ loops that truncate the stream. is faster to use :func:`list` instead of :func:`tee`. +.. function:: zip_longest(*iterables[, fillvalue]) + + Make an iterator that aggregates elements from each of the iterables. If the + iterables are of uneven length, missing values are filled-in with *fillvalue*. + Iteration continues until the longest iterable is exhausted. Equivalent to:: + + def zip_longest(*args, fillvalue=None): + # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- + def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): + yield counter() # yields the fillvalue, or raises IndexError + fillers = repeat(fillvalue) + iters = [chain(it, sentinel(), fillers) for it in args] + try: + for tup in zip(*iters): + yield tup + except IndexError: + pass + + If one of the iterables is potentially infinite, then the :func:`zip_longest` + function should be wrapped with something that limits the number of calls + (for example :func:`islice` or :func:`takewhile`). If not specified, + *fillvalue* defaults to ``None``. + + .. _itertools-example: Examples