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. If an iterable object is exhausted first, fillvalue is used to fill the remaining positions.
Technology Stack : The function is used to combine multiple iterable objects into an iterator. If an iterable object is exhausted first, fillvalue is used to fill the remaining positions.
Code Type : Built-in library functions
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 moves on to the next iterable, again until that is exhausted, and so on.
With multiple iterables, the iterator stops when the shortest iterable is exhausted.
For example:
>>> list(zip_longest([1, 2, 3], 'abc', [4, 5, 6]))
[(1, 'a', 4), (2, 'b', 5), (3, 'c', 6)]
>>> list(zip_longest([1, 2, 3], 'abc', [4, 5, 6], fillvalue=0))
[(1, 'a', 4), (2, 'b', 5), (3, 'c', 6), (0, 0, 0)]
>>> list(zip_longest([1, 2, 3], 'abc', [4, 5, 6], fillvalue='x'))
[(1, 'a', 4), (2, 'b', 5), (3, 'c', 6), ('x', 'x', 'x')]
"""
from itertools import zip_longest as itzip_longest
return itzip_longest(*iterables, fillvalue=fillvalue)