You can download this code by clicking the button below.
This code is now available for download.
This function implements a similar functionality to zip_longest. When the input sequences have different lengths, it fills the shorter sequences with fillvalue until all sequences are of the same length.
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
def fill_deques(deques, fillvalue):
for deque in deques:
while len(deque) < len(args[0]):
deque.append(fillvalue)
def zip_deques(*deques):
for deque in deques:
yield from deque
if not args:
return
max_length = max(len(arg) for arg in args)
deques = [deque(arg) for arg in args]
fill_deques(deques, fillvalue)
return list(zip_deques(*deques))