Combining Iterables with Fillvalue Using itertools.zip_longest

  • Share this:

Code introduction


Create a function that uses `itertools.zip_longest` to combine multiple iterable objects. If the iterators have different lengths, it fills the shorter ones with a `fillvalue`.


Technology Stack : Create a function that uses `itertools.zip_longest` to combine multiple iterable objects. If the iterators have different lengths, it fills the shorter ones with a `fillvalue`.

Code Type : Function

Code Difficulty : Intermediate


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

    def zip_longest(*args, fillvalue=()):
        iterator = itzip_longest(*args, fillvalue=fillvalue)
        return list(iterator)

    return zip_longest