Combining Iterables with Fillvalue

  • Share this:

Code introduction


The function is used to combine multiple iterable objects into an iterator. If the length of the iterables is inconsistent, the shorter iterable is padded with fillvalue.


Technology Stack : Built-in libraries

Code Type : Function

Code Difficulty :


                
                    
def zip_longest(*iterables, fillvalue=None):
    "Make an iterator that aggregates elements from each of the iterables.
    The shortest iterable is padded with fillvalue."
    iterators = [iter(it) for it in iterables]
    while True:
        yield [next(it, fillvalue) for it in iterators]

# JSON representation