You can download this code by clicking the button below.
This code is now available for download.
The function uses the `zip_longest` function from the `itertools` library to combine multiple iterable objects. If the iterable objects have different lengths, the missing parts are filled with the value specified by `fillvalue`.
Technology Stack : itertools
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=0):
"""
This function will return a zip object that aggregates elements from each of the iterables.
If the iterables are of uneven length, missing values in the shorter iterables are filled
with the value specified by fillvalue.
"""
from itertools import zip_longest
def _filler():
for fill in fillvalue:
yield fill
iterators = [iter(arg) for arg in args]
longest = max(iterators, key=len)
for item in longest:
for it in iterators:
yield next(it, next(_filler()))