You can download this code by clicking the button below.
This code is now available for download.
The function is used to combine multiple iterable objects into an iterator, and fill in missing values with fillvalue if the length of the iterable objects is inconsistent.
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 iterable
An iterable of iterables.
fillvalue : optional
The value to use for missing values in the iterables.
Returns
-------
iterator
An iterator that produces tuples containing elements from the iterables.
Examples
--------
>>> from itertools import zip_longest
>>> list(zip_longest([1, 2, 3], [4, 5], fillvalue=0))
[(1, 4, 0), (2, 5, 0), (3, None, 0)]
>>> list(zip_longest('abc', 'de', fillvalue='-'))
[('a', 'd', '-'), ('b', 'e', '-'), ('c', None, '-')]
"""
from itertools import zip_longest as _zip_longest
return _zip_longest(*iterables, fillvalue=fillvalue)