You can download this code by clicking the button below.
This code is now available for download.
The function returns an iterator that produces a series of tuples, where each tuple contains elements from different input iterables. If an iterable runs out of elements, it is padded with the fillvalue.
Technology Stack : zip_longest
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=0):
"""
This function will return a list of tuples, where the length of the longest input iterable is maintained.
Short iterables are padded with the fillvalue.
"""
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 result