[docs] [issue31270] Simplify documentation of itertools.zip_longest

Raphael Michel report at bugs.python.org
Fri Aug 25 05:33:42 EDT 2017

Raphael Michel added the comment:

Well, I could think of a way to still use repeat() here that also is pretty clean except for the fact that it fails if all inputs to zip_longest are repeat() iterators themselves (which would here lead to an empty iterator while it would otherwise lead to an infinite one):

    def zip_longest(*args, **kwds):
        # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
        fillvalue = kwds.get('fillvalue')
        iterators = [iter(it) for it in args]

        while True:
            values = []

            for i, it in enumerate(iterators):
                except StopIteration:
                    iterators[i] = repeat(fillvalue)

            if all(isinstance(it, repeat) for it in iterators):
                yield tuple(values)

Keeping chain() in use here just for the sake of using it is not worth it, I believe.


Python tracker <report at bugs.python.org>

More information about the docs mailing list