You can download this code by clicking the button below.
This code is now available for download.
The function is used to group elements from multiple iterable objects, filling with a specified fillvalue if one of the iterables is exhausted.
Technology Stack : Iterators (iterable)
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=None):
"""
Group elements of several iterables together while filling with fillvalue if an iterable is exhausted.
"""
iterators = [iter(arg) for arg in args]
while True:
result = []
for iterator in iterators:
try:
result.append(next(iterator))
except StopIteration:
result.append(fillvalue)
yield result