Combining Iterables with Fillvalue

  • Share this:

Code introduction


The function combines multiple iterable objects into an iterator. If some of the iterables are longer than others, fillvalue is used to fill the remaining positions.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    """
    Zip together multiple iterables but allow some of them to be shorter than others.
    Return an iterator that aggregates elements from each of the iterables.
    The iterator stops when the shortest iterable is exhausted, after which the remaining
    iterables are filled with fillvalue.
    """
    from itertools import zip_longest as _zip_longest
    return _zip_longest(*iterables, fillvalue=fillvalue)

# JSON output                
              
Tags: