Zip Longest with Fillvalue Implementation

  • Share this:

Code introduction


This function merges multiple iterable objects. If an iterable object has been traversed out, fillvalue is used to fill


Technology Stack : zip_longest

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    iters = [iter(arg) for arg in args]
    while True:
        result = []
        for i, it in enumerate(iters):
            try:
                result.append(next(it))
            except StopIteration:
                result.append(fillvalue)
        yield tuple(result)                
              
Tags: