You can download this code by clicking the button below.
This code is now available for download.
This function mimics the behavior of the zip function but can handle sequences of different lengths. When the sequences are of unequal length, it fills the shorter ones with a fillvalue.
Technology Stack : Built-in function zip, iterators
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=()):
"""
类似于zip函数,但会返回一个迭代器,其中较短的输入序列会用fillvalue填充。
"""
iters = [iter(arg) for arg in args]
while True:
yield tuple(next(it, fillvalue) for it in iters)