Custom Implementation of itertools.zip_longest

  • Share this:

Code introduction


This function implements a similar function as itertools.zip_longest, which can merge multiple iterators. If the iterators have different lengths, it uses fillvalue to fill.


Technology Stack : itertools, collections

Code Type : Iterator

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    """
    Zip equal length sequences together into a list of tuples, shorter sequences
    are padded with fillvalue. Sequence lengths are determined by the longest
    input sequence.
    """
    from itertools import zip_longest as zip_longest
    from collections import deque

    def interleave(*iterables):
        sentinel = object()
        iterators = [deque(iterable) for iterable in iterables]
        while True:
            result = []
            for iterator in iterators:
                element = next(iterator, sentinel)
                if element is sentinel:
                    return result
                result.append(element)
            yield tuple(result)

    def interleave_fill(*iterables, fillvalue):
        sentinel = object()
        iterators = [deque(iterable) for iterable in iterables]
        while True:
            result = []
            for iterator in iterators:
                element = next(iterator, sentinel)
                if element is sentinel:
                    result.extend([fillvalue] * (len(result) + 1))
                    return tuple(result)
                result.append(element)
            yield tuple(result)

    return list(interleave_fill(*args, fillvalue=fillvalue))