Enhanced Zip Function with Fill Values

  • Share this:

Code introduction


Creates a function similar to the built-in zip function, but fills the output list with fill values if the input iterables are of unequal lengths.


Technology Stack : itertools, collections

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=()):
    """
    Like zip(), but the output list is filled with fillvalues if the iterables are of
    unequal length.
    """
    from itertools import zip_longest
    from collections import deque
    return [deque(x) for x in zip_longest(*args, fillvalue=())]