You can download this code by clicking the button below.
This code is now available for download.
This function creates a new iterator from an arbitrary number of iterable objects. If any iterable is exhausted, it continues iteration with a specified fill value.
Technology Stack : Iterator, generator, tuple unpacking
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=0):
iters = [iter(args[i]) for i in range(len(args))]
while True:
result = []
for i, it in enumerate(iters):
try:
result.append(next(it))
except StopIteration:
iters[i] = iter([fillvalue])
result.append(next(iters[i]))
yield result