Enhanced Zip Function with Fillvalue Support

  • Share this:

Code introduction


The function takes multiple iterable objects as input and returns an iterator that produces tuples, each containing the next value from each input iterable. If an iterable is exhausted first, the value at that position is filled with the specified `fillvalue`.


Technology Stack : The function takes multiple iterable objects as input and returns an iterator that produces tuples, each containing the next value from each input iterable. If an iterable is exhausted first, the value at that position is filled with the specified `fillvalue`.

Code Type : Iterator Tool Functions

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    """
    Like zip() but the zip object will keep adding values until the shortest iterable is exhausted.
    """
    from itertools import zip_longest as zip_longest_imp
    return zip_longest_imp(*iterables, fillvalue=fillvalue)