Merging Iterables with Fillvalue

  • Share this:

Code introduction


The function is used to merge multiple iterable objects into one iterator. If the iterables are of uneven length, the specified value is used to fill in the shorter iterables.


Technology Stack : itertools (built-in library)

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    """
    This function will return a zip object that aggregates elements from each of the iterables.
    If the iterables are of uneven length, missing values are filled-in with the fillvalue.
    """
    from itertools import zip_longest

    return zip_longest(*args, fillvalue=fillvalue)