You can download this code by clicking the button below.
This code is now available for download.
This function takes any number of iterable objects as arguments and yields a tuple sequence based on the length of the shortest input sequence. If an input sequence is exhausted, it is filled with fillvalue.
Technology Stack : Built-in library: collections
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=0):
"""
Similar to the zip function, but the shortest input sequence is extended by fillvalues if the input
sequences are of unequal length.
"""
iters = [iter(arg) for arg in args]
while True:
result = []
for iter_ in iters:
try:
result.append(next(iter_))
except StopIteration:
result.append(fillvalue)
yield tuple(result)