[Python-ideas] Add 'interleave' function to itertools?
Vito De Tullio
vito.detullio at gmail.com
Wed Aug 7 21:19:19 CEST 2013
MRAB wrote:
> def interleave(*iterables):
> """Return a interleave object whose .__next__() method returns an
> element from each iterable argument in turn. The .__next__()
> method continues until the shortest iterable in the argument
> sequence is exhausted and then it raises StopIteration.
> """
>
> sources = [iter(iterable) for iterable in iterables]
>
> try:
> while True:
> for iterable in sources:
> yield next(iterable)
> except StopIteration:
> pass
isn't something like
def interleave(*iterables):
for zipped_iterables in zip(*iterables):
yield from zipped_iterables
sufficient?
(and the same for interleave_longest / zip_longest)
--
By ZeD
More information about the Python-ideas
mailing list