You can download this code by clicking the button below.
This code is now available for download.
This function provides similar functionality to the built-in zip function, but it fills the shorter iterable with a specified fillvalue if the iterables are of unequal length.
Technology Stack : Built-in library: itertools
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*iterables, fillvalue=None):
"""
Like zip(), but the shortest iterable is extended by fillvalues if needed.
"""
# This function is using the built-in `zip()` function and the `itertools.zip_longest()` function
from itertools import zip_longest
def aggregate(lst):
return [x for x in lst if x is not fillvalue]
zipped = zip_longest(*iterables, fillvalue=fillvalue)
for i in range(len(zipped)):
zipped[i] = aggregate(zipped[i])
return zipped
# JSON representation