Merging Iterables with Fillvalue in Python

  • Share this:

Code introduction


The function merges multiple iterable objects into an iterator, filling missing values with the value specified by the fillvalue parameter if the number of elements in one of the iterable objects is less than the others.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    """Zips the input iterables together, filling missing values with fillvalue.

    Args:
        *args: An arbitrary number of input iterables.
        fillvalue: The value to use for missing values.

    Returns:
        An iterator that aggregates elements from each of the iterables.

    """
    from itertools import zip_longest
    return zip_longest(*args, fillvalue=fillvalue)                
              
Tags: