Zip Longest Function: Combining Iterators with Fillvalue

  • Share this:

Code introduction


This function takes multiple iterable objects as arguments and returns a list of tuples. If an iterable is exhausted, the remaining tuples are filled with the fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    """
    从迭代器中返回元组列表,如果其中一个迭代器耗尽,则使用fillvalue填充。
    """
    # 使用itertools.zip_longest来组合多个迭代器,如果某个迭代器耗尽,使用fillvalue填充
    from itertools import zip_longest
    return list(zip_longest(*iterables, fillvalue=fillvalue))                
              
Tags: