Combining Iterables with zip_longest in itertools

  • Share this:

Code introduction


This function uses the zip_longest function from the itertools library to combine multiple iterable objects. If the lengths of these objects are different, a specified fillvalue is used to fill in the missing parts.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=0):
    # 使用 itertools.zip_longest 来组合多个可迭代对象,如果长度不等,用 fillvalue 填充
    from itertools import zip_longest
    return zip_longest(*args, fillvalue=fillvalue)                
              
Tags: