You can download this code by clicking the button below.
This code is now available for download.
The function is used to combine multiple iterable objects into an iterator. If the length of the iterable objects is different, the fillvalue is used to fill the shorter sequence.
Technology Stack : itertools, collections
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=0):
from itertools import zip_longest
from collections import deque
longest = max(map(len, args), default=0)
result = []
for i in range(longest):
batch = [deque(arg)[i:] for arg in args]
result.append([item for item in zip_longest(*batch, fillvalue=fillvalue) if item is not fillvalue])
return result