Packing Lists into Tuples with Fillvalue

  • Share this:

Code introduction


This function is used to pack a variable number of list arguments into a list of tuples. If the elements of a list run out, the remaining lists are filled with the specified fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=()):
    """将可变数量的参数列表打包成元组列表,如果最短的输入列表用完,剩余的列表会用fillvalue填充。

    Args:
        *args: 可变数量的列表参数。
        fillvalue: 用于填充较短列表的值。

    Returns:
        一个列表,其中包含元组,每个元组包含从每个列表中取出的元素。
    """
    from itertools import zip_longest as it_zip_longest

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