Python Emulation of itertools.zip_longest Functionality

  • Share this:

Code introduction


This function emulates the functionality of the built-in `itertools.zip_longest` module, which can handle iterables of uneven length and fill missing values with `fillvalue`.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    # This function will return a list of tuples, zipping the input iterables together.
    # If the iterables are of uneven length, missing values in the shorter iterables will be filled with fillvalue.
    
    from itertools import zip_longest

    def my_zip_longest(*args, fillvalue=None):
        return list(zip_longest(*args, fillvalue=fillvalue))

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