You can download this code by clicking the button below.
This code is now available for download.
This function is similar to the built-in zip() function, but it fills the shorter iterables with fillvalue until all iterables are exhausted if they are of unequal length.
Technology Stack : zip_longest
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=None):
"""
Like zip() but merges the rest of the iterables with fillvalues.
"""
iterators = [iter(arg) for arg in args]
while True:
result = []
for it in iterators:
try:
result.append(next(it))
except StopIteration:
result.append(fillvalue)
yield tuple(result)