Combining Iterables with FillValue

  • Share this:

Code introduction


Combines multiple iterable objects into a list of tuples. If an iterable is shorter than the others, it is filled with the fillvalue until all iterables have the same length.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=()):
    """将可迭代对象组合成元组列表,如果某个可迭代对象较短,则使用fillvalue填充。

    Args:
        *args: 可迭代对象列表。
        fillvalue: 填充值,用于填充较短的迭代对象。

    Returns:
        元组列表。
    """
    from itertools import zip_longest
    return list(zip_longest(*args, fillvalue=fillvalue))                
              
Tags: