Combining Iterables with FillValue

  • Share this:

Code introduction


This function takes any number of iterable objects as arguments and combines them into a new iterator. If some iterables are shorter than others, the missing values are filled in with the specified fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    # This function will zip the input iterables (args) and fill in missing values with the specified fillvalue.
    # It uses the itertools.zip_longest function from the itertools module.

    from itertools import zip_longest

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