Zip Longest Function Explanation

  • Share this:

Code introduction


This function takes an arbitrary number of iterable objects as arguments and zips them together into an iterator. If the iterables are of different lengths, the shorter ones are filled with fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=0):
    from itertools import zip_longest

    def inner_zip_longest(*iterables, fillvalue=0):
        # Zip the iterables together, filling missing values with fillvalue
        return zip_longest(*iterables, fillvalue=fillvalue)

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