You can download this code by clicking the button below.
This code is now available for download.
Zips the elements of multiple iterables (sequences) into a single iterable of tuples. If the iterables are of unequal lengths, missing values are filled in with fillvalue.
Technology Stack : itertools
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*iterables, fillvalue=None):
"""
Zip the elements of multiple iterables (sequences) into a single iterable of tuples.
The *iterables argument is a variable number of iterables. If the iterables are of
unequal lengths, missing values are filled-in with fillvalue. Default is None.
Args:
*iterables: An arbitrary number of iterables.
fillvalue: Value to use for missing values if the iterables are of unequal lengths.
Returns:
An iterator of tuples, length equal to the longest iterable.
Example:
>>> list(zip_longest([1, 2, 3], [4, 5], [6], fillvalue=0))
[(1, 4, 6), (2, 5, None), (3, None, None)]
"""
from itertools import zip_longest as _zip_longest
return _zip_longest(*iterables, fillvalue=fillvalue)