You can download this code by clicking the button below.
This code is now available for download.
This function combines variable number of sequences into a new sequence. If a sequence is shorter than the others, it is filled with fillvalue. If all sequences are of the same length, it returns the combined sequence directly.
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
def ensure_length(seq, length):
return deque(seq, maxlen=length)
max_length = max(len(arg) for arg in args)
zipped = zip_longest(*args, fillvalue=fillvalue)
return tuple(ensure_length(list(item), max_length) for item in zipped)