You can download this code by clicking the button below.
This code is now available for download.
The function combines multiple iterable objects into an iterator. If some iterable objects have different lengths, it fills the missing parts with fillvalue.
Technology Stack : Iterators, Generators
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=0):
iters = [iter(arg) for arg in args]
while True:
result = []
for it in iters:
try:
result.append(next(it))
except StopIteration:
result.append(fillvalue)
if len(result) == 1:
yield result[0]
else:
yield result