Combining Iterables with FillValue in Python

  • Share this:

Code introduction


This function is used to combine multiple iterable objects (such as lists, tuples, etc.) into a list of tuples. If the iterables have different lengths, missing values in the shorter iterables will be filled with fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    # This function will zip together several iterables (args) and return a list of tuples.
    # If the iterables are of unequal length, missing values in the shorter iterables will be filled with fillvalue.

    from itertools import zip_longest

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