Combining Iterables with Fillvalue

  • Share this:

Code introduction


This function combines multiple iterable objects (such as lists or tuples) together, filling in missing values with a specified fillvalue when the lengths of the iterables are unequal.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    # This function zips multiple iterables (like lists or tuples) together, 
    # filling in missing values with a specified fillvalue when the iterables are of unequal length.

    # Use itertools.zip_longest to achieve the same functionality as the built-in zip_longest
    from itertools import zip_longest

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