Zip Longest Iterator with Fillvalue

  • Share this:

Code introduction


This function accepts multiple iterable objects as arguments and uses `itertools.zip_longest` to create an iterator that returns a tuple. The tuple contains elements from different iterable objects, and if an element from an iterable object is exhausted, it is filled with the value specified by the `fillvalue` parameter.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


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

    def generate_tuples():
        return zip_longest(*args, fillvalue=fillvalue)

    def create_zip():
        for tup in generate_tuples():
            yield tup

    return create_zip()                
              
Tags: