Combining Iterables with Fillvalue

  • Share this:

Code introduction


The function is used to combine multiple iterable objects into an iterator. When the shortest iterable is exhausted, missing values in other iterables will be filled with fillvalue until all iterables are exhausted.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    "zip(*iterables) -> zip object; see help(zip) for usage examples"
    # This function will return an iterator that aggregates elements from each of the iterables.
    # It will stop when the shortest iterable is exhausted, filling in missing values with fillvalue until all iterables are exhausted.
    from itertools import zip_longest as _zip_longest
    return _zip_longest(*iterables, fillvalue=fillvalue)                
              
Tags: