[Python-ideas] Add 'interleave' function to itertools?

Serhiy Storchaka storchaka at gmail.com
Wed Aug 7 22:02:14 CEST 2013


07.08.13 22:19, Vito De Tullio написав(ла):
> 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?

chain.from_iterable(zip(*iterables))




More information about the Python-ideas mailing list