You can download this code by clicking the button below.
This code is now available for download.
This function provides functionality similar to `itertools.zip_longest`, merging multiple iterable objects into one iterator. If some iterable objects are shorter, they are padded with `fillvalue`.
Technology Stack : itertools, collections
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=None):
from itertools import zip_longest
from collections import deque
# Ensure all arguments are of equal length by padding shorter ones with fillvalue
longest = max(len(list(arg)) for arg in args)
for i, arg in enumerate(args):
args[i] = deque(arg, maxlen=longest)
# Zip together the deques, filling in with fillvalue where necessary
return zip_longest(*args, fillvalue=fillvalue)