Combining Iterables with Zip Longest

  • Share this:

Code introduction


This function takes multiple iterable objects and combines them into an iterator. If the iterables have different lengths, missing values are filled with `fillvalue`.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    # This function takes several iterables (at least two) and returns an iterator that aggregates elements from each of the iterables.
    # If the iterables are of uneven length, missing values are filled-in with `fillvalue`.

    from itertools import zip_longest
    import itertools

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