Merging Iterables with FillValue

  • Share this:

Code introduction


This function merges multiple iterable objects. If an iterable is shorter, it is filled with the specified fillvalue until the longest iterable is finished.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    # 将多个可迭代对象合并,如果某个可迭代对象较短,则用fillvalue填充
    # 使用内置库itertools

    from itertools import zip_longest

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