Combining Iterables with Fillvalue

  • Share this:

Code introduction


This function combines multiple iterable objects into an iterator. If the iterables have different lengths, it fills in the missing values with fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    """
    Returns an iterator that aggregates elements from each of the iterables.
    The iterator returns pairs, with the i-th pair containing the i-th element
    from each of the iterables. If the iterables are of uneven length, missing values
    in the shorter iterables are filled with fillvalue.
    """
    from itertools import zip_longest as zip_longest_itertools
    iterator = zip_longest_itertools(*iterables, fillvalue=fillvalue)
    return iterator                
              
Tags: