Zip Longest Function for Merging Iterables

  • Share this:

Code introduction


This function is used to merge multiple iterable objects into a single iterator. If one of the iterable objects is exhausted, fillvalue is used to fill the remaining positions.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    # 将多个可迭代对象合并为一个新的迭代器,如果其中一个可迭代对象被耗尽,则用fillvalue填充
    from itertools import zip_longest as it_zip_longest

    def wrapper(*iterables, fillvalue=None):
        return it_zip_longest(*iterables, fillvalue=fillvalue)

    return wrapper                
              
Tags: