Zip Longest Function for Merging Iterables with FillValue

  • Share this:

Code introduction


The function merges multiple iterable objects into a single iterator. If one of the iterable objects is longer than the others, it uses fillvalue to fill in the shorter iterable.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=0):
    # 将一系列可迭代对象合并为一个迭代器,如果某个可迭代对象较短,则使用fillvalue填充
    from itertools import zip_longest as zip_longest_imp
    return zip_longest_imp(*args, fillvalue=fillvalue)                
              
Tags: