Combining Iterables with Fillvalue

  • Share this:

Code introduction


This function accepts multiple iterable objects as parameters and combines them into a single iterable that stops when the shortest iterable is exhausted. If an iterable runs out prematurely, the fillvalue parameter is used to fill in the missing values.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=0):
    """Combine several iterables into one iterable that stops when the shortest iterable is exhausted.

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

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

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