You can download this code by clicking the button below.
This code is now available for download.
The function implements a similar functionality to the zip function but can handle iterables of uneven length. If an iterable is exhausted, missing values are filled with fillvalue.
Technology Stack : Built-in library
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*iterables, fillvalue=None):
"zip_longest(*iterables, fillvalue=fillvalue) -> zip object"
"Returns an iterator that aggregates elements from each of the iterables.
The iterator returns pairs from the iterables. If the iterables are of uneven
length, missing values are filled-in with fillvalue. Default is fillvalue=None."
iterators = [iter(it) for it in iterables]
for i in iterators:
next(i, None)
while True:
result = []
for it in iterators:
result.append(next(it, fillvalue))
if None in result:
return
yield tuple(result)