Combining Iterables with Fillvalue

  • Share this:

Code introduction


The function combines multiple iterable objects. If a particular iterable runs out of elements, it fills in the missing values with the specified fillvalue.


Technology Stack : Built-in libraries

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=0):
    # Zip the input iterables together and fill in missing values with fillvalue
    iters = [iter(arg) for arg in args]
    while True:
        yield [next(it, fillvalue) for it in iters]