You can download this code by clicking the button below.
This code is now available for download.
The function is used to merge multiple iterable objects. If the iterable objects have different lengths, fillvalue is used to fill the missing values.
Technology Stack : zip_longest, iterator
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 missing values
in place with fillvalue until the longest iterable is exhausted.
Args:
*iterables: An arbitrary number of iterables.
fillvalue: The value to use for missing values if the iterables are of unequal
lengths.
Returns:
An iterator that aggregates elements from the iterables.
"""
iterators = [iter(it) for it in iterables]
while True:
yield [next(it, fillvalue) for it in iterators]