Aggregating Elements from Iterables with Fillvalue

  • Share this:

Code introduction


This function is used to aggregate elements from multiple iterable objects. If the iterables are of uneven length, missing values are filled with fillvalue.


Technology Stack : itertools

Code Type : Iterator generator

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    """Create an iterator that aggregates elements from each of the iterables.
    
    The iterator returns pairs of elements from the iterables. If the iterables are
    of uneven length, missing values are filled-in with fillvalue. Default is None.
    
    Args:
        *iterables: An arbitrary number of iterables.
        fillvalue: Value to use for missing values if the iterables are of uneven length.

    Returns:
        An iterator that aggregates elements from each of the iterables.
    """
    from itertools import zip_longest

    return zip_longest(*iterables, fillvalue=fillvalue)                
              
Tags: