Merging Iterables with Fillvalue

  • Share this:

Code introduction


This function is used to merge multiple iterable objects into an iterator. It fills missing values with fillvalue when the shortest iterable is exhausted.


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."
    "The iterator returns pairs of the form (i, val), where i is the index of the
    element from the iterable and val is the element obtained from that iterable."
    "The iterator stops when the shortest iterable is exhausted, filling missing
    values with fillvalue."
    from itertools import zip_longest as zip_longest_imp

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