You can download this code by clicking the button below.
This code is now available for download.
The function is used to merge multiple iterable objects into an iterator. If one of the iterable objects is exhausted, the elements of the other iterable objects will fill into the result, and the value not provided will be specified as fillvalue.
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.
The iterator returns elements from the first iterable until it is exhausted,
then it fills from the second iterable, and so on.
If the iterables are of uneven length, missing values are filled-in with fillvalue.
Examples:
>>> list(zip_longest('ABC', 'X', 'MNO', fillvalue='-'))
['A', 'X', 'M', 'N', 'O', 'P']
>>> list(zip_longest('ABC', 'X', 'MNO', fillvalue='*'))
['A', 'X', '*', 'M', 'N', 'O', '*']
>>> list(zip_longest('ABC', 'X', 'MNO', fillvalue=()))
['A', 'X', 'M', 'N', 'O', ()]
>>> list(zip_longest('ABC', 'X', 'MNO', fillvalue=0))
['A', 'X', 'M', 'N', 'O', 0]
"""
from itertools import zip_longest as _zip_longest
return _zip_longest(*iterables, fillvalue=fillvalue)