You can download this code by clicking the button below.
This code is now available for download.
This function zips multiple iterable objects into a single iterable. If the iterables are of uneven length, the specified value is used to fill in the missing values.
Technology Stack : itertools, collections
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=0):
"""
Zip the elements of the provided iterables (args) together into a single iterable.
If the iterables are of uneven length, missing values are filled with the fillvalue.
"""
from itertools import zip_longest
from collections import deque
iterators = [deque(iter(i)) for i in args]
result = []
while iterators:
result.append([next(iter, fillvalue) for iter in iterators if iter])
iterators = [deque(list(iter)) for iter in iterators if iter]
return result