Combining Iterables with FillValue Using itertools.zip_longest

  • Share this:

Code introduction


This function uses `itertools.zip_longest` to combine multiple iterable objects. If one iterable has been traversed, `fillvalue` is used to fill the remaining positions.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=0):
    zip_longest = itertools.zip_longest(*args, fillvalue=fillvalue)
    for values in zip_longest:
        yield values                
              
Tags: