Combining Iterables with Variable Lengths

  • Share this:

Code introduction


This function combines multiple iterable objects together. If they are of unequal length, it fills in the missing parts with fillvalue.


Technology Stack : itertools, zip_longest

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    """
    Zip equal length sequences together, but allow each sequence to be of different lengths.
    If the sequences are of unequal length, missing values are filled-in with fillvalue.
    Default fillvalue is None.
    """
    from itertools import zip_longest
    import itertools

    return list(zip_longest(*iterables, fillvalue=fillvalue))