Hey,

You can always do `itertools.chain.from_iterable(zip(iterable, itertools.repeat(sep)))` but I agree that it is verbose.

Cheers,

E

On Wed, 9 Dec 2020 at 04:16, <aurelien.lambert.89@gmail.com> wrote:
Hi

I like using itertools for creating long strings while not paying the cost of intermediate strings (by eventually calling str.join on the whole iterator).
However, one missing feature is to mimic the behavior of str.join as an iterator: an iterator that returns the items of an iterable, separated by the separator.
I suggest name "interleave" or "join" (whichever is the most clear / least ambigous).

        def interleave(sep, iterable):
                """
                Makes an iterator that returns elements from an iterable, separated by the separator.
                """
                notfirst = False
                for i in iterable:
                        if notfirst:
                                yield sep
                        else:
                                notfirst = True
                        yield i

Could imagine a more elaborate implementation that can take several iterators, and would be equivalent to
        lambda chain_zip_interleave sep, *iterables: itertools.chain.from_iterable(interleave((sep,), zip(*iterables)))
But that may be seriously overkill, and I have hard time describing it.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/YWT5BVGPNO3UBW4DZYYPXVCJY2JH7B4H/
Code of Conduct: http://python.org/psf/codeofconduct/