You can download this code by clicking the button below.
This code is now available for download.
The function implements a similar function to zip, but it can use fillvalue to fill in missing values when the length of iterators is inconsistent.
Technology Stack : itertools
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=None):
iters = [iter(arg) for arg in args]
while True:
result = []
for i, it in enumerate(iters):
try:
result.append(next(it))
except StopIteration:
result.append(fillvalue)
if len(result) == 1 and result[0] == fillvalue:
break
yield tuple(result)