Merging Iterables with zip_longest

  • Share this:

Code introduction


The zip_longest function is used to merge multiple iterable objects into an iterator. If the length of the iterators is uneven, missing values are filled in with fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    "zip_longest(*iterables, fillvalue=None) -> zip object"
    "Return an iterator that aggregates elements from each of the iterables.
    The iterator returns pairs from the iterables. If the iterables are of uneven
    length, missing values are filled-in with fillvalue. Default is fillvalue=None."
    from itertools import zip_longest as zip_longest
    return zip_longest(*iterables, fillvalue=fillvalue)                
              
Tags: