Combining Iterables with Fillvalue

  • Share this:

Code introduction


This function combines multiple iterable objects into an iterator. If an iterable runs out, it is filled with fillvalue.


Technology Stack : Generator, iterator

Code Type : Generator function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    zip_longest_args = args
    iterator = iter(zip_longest_args)
    while True:
        yield (next(iterator, fillvalue) for _ in zip_longest_args)