You can download this code by clicking the button below.
This code is now available for download.
The function takes multiple iterable objects and returns an iterator that aggregates elements from each of the iterables. The iterator stops when the shortest iterable is exhausted, filling in missing values with fillvalue until all iterables are exhausted.
Technology Stack : Built-in library
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*iterables, fillvalue=None):
"""
Return an iterator that aggregates elements from each of the iterables.
The iterator stops when the shortest iterable is exhausted, filling in missing values with fillvalue until all iterables are exhausted.
"""
iterators = [iter(it) for it in iterables]
while True:
for i, it in enumerate(iterators):
try:
yield next(it)
except StopIteration:
iterators[i] = iter([fillvalue])