You can download this code by clicking the button below.
This code is now available for download.
The function implements the functionality similar to `itertools.zip_longest`, which can take multiple iterable objects. If one of the iterable objects runs out, it will fill the remaining positions with `fillvalue`.
Technology Stack : collections.deque, itertools, zip
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=None):
from itertools import zip_longest
from collections import deque
def _unzip_longest(iterable):
iterator = iter(iterable)
for element in iterator:
yield element
try:
yield next(iterator)
except StopIteration:
pass
def _iterables_longest(*iterables):
iters = [deque(iterable) for iterable in iterables]
while iters:
result = []
for it in iters:
try:
result.append(it.popleft())
except IndexError:
result.append(fillvalue)
yield tuple(result)
return _iterables_longest(*args)