You can download this code by clicking the button below.
This code is now available for download.
The function combines multiple iterable objects into an iterator. If one of the iterable objects is exhausted, a specified fill value is used to fill the other iterable objects.
Technology Stack : itertools, collections
Code Type : Iterator
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=0):
from itertools import zip_longest
from collections import deque
def fill_deques(deques, fill_value):
for deque_ in deques:
while len(deque_) < len(args):
deque_.append(fill_value)
iters = [iter(arg) for arg in args]
fill_deques(iters, fillvalue)
while True:
try:
yield tuple(arg.next() for arg in iters)
except StopIteration:
break