You can download this code by clicking the button below.
This code is now available for download.
The function accepts multiple iterable objects as arguments and generates an iterator that aggregates elements from each of the iterables. It returns a list as soon as the shortest iterable is exhausted, filling the rest with fillvalue.
Technology Stack : Iterator, generator, iteration
Code Type : Iterator generator
Code Difficulty : Intermediate
def zip_longest(*iterables, fillvalue=None):
"Make an iterator that aggregates elements from each of the iterables."
"It returns a list as soon as the shortest iterable is exhausted, filling
the rest with fillvalue."
iterators = [iter(it) for it in iterables]
while True:
result = []
for it in iterators:
try:
result.append(next(it))
except StopIteration:
result.append(fillvalue)
if len(result) == 1 and result[0] is fillvalue:
break
yield result