Generator for Merging Iterables with Fillvalue

  • Share this:

Code introduction


Create a generator that merges multiple iterable objects. If the lengths are not consistent, use fillvalue to fill in the missing parts.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


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

    def generator():
        for iterable in args:
            for element in iterable:
                yield element
        while True:
            for fill in fillvalue:
                yield fill

    return list(generator())                
              
Tags: