Zip Longest Function with Fillvalue

  • Share this:

Code introduction


The function merges multiple iterable objects into a single list, filling shorter lists with fillvalue if the lengths are not consistent.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    """
    Like zip() but merges lists of different lengths into a single list by filling with fillvalue.
    """
    # Using zip_longest from itertools, which is a built-in module
    from itertools import zip_longest

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

# JSON representation of the function                
              
Tags: