Combining Iterables with Fillvalue Using zip_longest

  • Share this:

Code introduction


The zip_longest function is used to combine multiple iterable objects into a single iterator. If one of the iterable objects finishes traversing, the remaining elements are filled with fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    # 将可变数量的列表合并为一个新的列表,如果最短的列表先遍历完,剩余的列表将继续遍历,用fillvalue填充
    from itertools import zip_longest as zip_longest_imp
    return list(zip_longest_imp(*args, fillvalue=fillvalue))                
              
Tags: