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 -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/YWT5BVGPNO3UBW4DZYYPXVCJY2JH7B4H/
Code of Conduct: http://python.org/psf/codeofconduct/