Combining Iterables with Fillvalue

  • Share this:

Code introduction


The function is used to combine a variable number of iterable objects into an iterator. If the iterables are of different lengths, it fills the shorter ones with a fillvalue.


Technology Stack : zip_longest, iterable, iterator, fill value

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    zip_longest_list = zip_longest(*args, fillvalue=fillvalue)
    for item in zip_longest_list:
        if fillvalue in item:
            yield item[:-1]
        else:
            yield item