You can download this code by clicking the button below.
This code is now available for download.
The function aggregates elements from each of the provided iterable objects. If the iterables have different lengths, missing values are filled with a specified value.
Technology Stack : itertools
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*iterables, fillvalue=None):
"Make an iterator that aggregates elements from each of the iterables.
If the iterables are of uneven length, missing values are filled-in with fillvalue.
Parameters
----------
iterables : sequence or iterator
An iterable series of iterables.
fillvalue : optional
The value to use for missing values if the iterables are of uneven length.
Examples
--------
>>> list(zip_longest([1, 2, 3], 'abc', fillvalue=-1))
[(1, 'a', -1), (2, 'b', -1), (3, 'c', -1)]
>>> list(zip_longest([1, 2, 3, 4], 'abc', fillvalue='x'))
[(1, 'a', None), (2, 'b', None), (3, 'c', None), (4, 'd', 'x')]
>>> list(zip_longest('abc', 'def', fillvalue=0))
[('a', 'd', 0), ('b', 'e', 0), ('c', 'f', 0)]
"""
from itertools import zip_longest as _zip_longest
return _zip_longest(*iterables, fillvalue=fillvalue)