Combining Iterables with Optional Fill Values

  • Share this:

Code introduction


The function is used to combine multiple iterable objects into tuples. If the iterable objects are of unequal lengths, the missing parts can be filled with a specified value.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    "zip(*iterables) -> zip object whose items are tuples of the input iterables"
    "If the iterables are of uneven length, missing values are filled-in with fillvalue"
    "from the right."
    # Using 'itertools.zip_longest' from the itertools module
    from itertools import zip_longest
    return zip_longest(*iterables, fillvalue=fillvalue)                
              
Tags: