Extended Zip Function with Fillvalue

  • Share this:

Code introduction


The function accepts any number of iterable objects and returns an iterator that returns a tuple containing elements from each iterable. If an element is missing in an iterable, it is filled with fillvalue. When the longest iterable is exhausted, the shorter iterables continue to be filled with fillvalue.


Technology Stack : Built-in function zip_longest

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    zip_longest = zip_longest(*iterables, fillvalue=fillvalue)
    for i in iterables:
        while len(i) < max(len(j) for j in iterables):
            i.append(fillvalue)
    return zip_longest

# JSON representation