Zip Multiple Iterables into Tuples with Fillvalue

  • Share this:

Code introduction


This function zips multiple iterable objects of equal length into a list of tuples, shorter iterables will be padded with fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    """
    Zip equal length sequences together into a list of tuples, shorter sequences
    will be padded with fillvalue.
    """
    # 使用内置的zip函数来组合多个可迭代对象
    # 使用itertools.zip_longest来处理长度不等的情况,用fillvalue填充
    from itertools import zip_longest
    return list(zip_longest(*args, fillvalue=fillvalue))

# JSON格式的描述                
              
Tags: