Combining Iterables with Fillvalue Using zip_longest

  • Share this:

Code introduction


This function utilizes `itertools.zip_longest` to combine multiple iterable objects. If these iterable objects are of unequal length, it fills in the missing values with `fillvalue`.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=0):
    # This function zips multiple iterables (args) together, filling in missing values with fillvalue
    # where the iterables are of unequal length.

    from itertools import zip_longest as _zip_longest

    def _zip_longest(*args, fillvalue=0):
        return _zip_longest(*args, fillvalue=fillvalue)

    return _zip_longest(*args, fillvalue=fillvalue)                
              
Tags: