You can download this code by clicking the button below.
This code is now available for download.
This function implements similar functionality to itertools.zip_longest, merging multiple iterable objects into an iterator. If the iterables are of unequal length, it fills the shorter ones with fillvalue.
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):
deque_.append(fillvalue)
def generate():
deques = [deque(arg) for arg in args]
fill_deques(deques, fillvalue)
while True:
yield [deque_.popleft() for deque_ in deques]
fill_deques(deques, fillvalue)
return generate()