Aggregating Iterators from Multiple Sources with Fillvalue

  • Share this:

Code introduction


The function is used to merge multiple iterable objects and return an iterator that takes elements from each of the iterables until the longest iterable is exhausted. If the iterables are of unequal length, fillvalue is used to fill in missing values.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    "Make an iterator that aggregates elements from each of the iterables."
    "It returns elements from the iterables up to the longest one."
    "The iterator stops when the shortest iterable is exhausted."
    "fillvalue is used for missing values if the iterables are of unequal length."
    from itertools import zip_longest as zip_longest_imp
    return zip_longest_imp(*iterables, fillvalue=fillvalue)                
              
Tags: