Combining Iterables with FillValue in zip_longest

  • Share this:

Code introduction


The zip_longest function combines multiple iterable objects. If an object is exhausted, it is filled with the fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    """zip_longest函数用于合并多个可迭代对象,如果最短的迭代对象先遍历完,则使用fillvalue填充剩余的迭代对象。

    Args:
        *args: 可变数量的可迭代对象。
        fillvalue: 填充值,用于最短的迭代对象遍历完后。

    Returns:
        zip对象,包含所有迭代对象的元素,如果某个迭代对象遍历完,则使用fillvalue填充。

    """
    from itertools import zip_longest

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