Python Programming - Generators - Discussion

Discussion Forum : Generators - General Questions (Q.No. 98)
98.
How does the itertools.zip_longest() function behave when used with generators of unequal lengths?
It raises a TypeError
It stops when the shortest generator is exhausted
It raises a ValueError
It continues until the longest generator is exhausted, filling in missing values with a specified fillvalue
Answer: Option
Explanation:
itertools.zip_longest() continues until the longest iterable is exhausted, filling in missing values with a specified fillvalue.
import itertools

gen1 = (1, 2, 3)
gen2 = ('a', 'b')

zipped_iterable = itertools.zip_longest(gen1, gen2, fillvalue=None)

print(list(zipped_iterable))  # Output: [(1, 'a'), (2, 'b'), (3, None)]
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.