Python Programming - Generators - Discussion
Discussion Forum : Generators - General Questions (Q.No. 77)
77.
How does the
itertools.chain.from_iterable()
function differ from itertools.chain()
when used with generators?
Answer: Option
Explanation:
itertools.chain.from_iterable()
is designed to handle nested iterables, flattening them into a single sequence. itertools.chain()
concatenates multiple generators into a single sequence.
import itertools
nested_generator = ([1, 2, 3], [4, 5, 6], [7, 8, 9])
flattened_iterable = itertools.chain.from_iterable(nested_generator)
concatenated_iterable = itertools.chain(*nested_generator)
print(list(flattened_iterable)) # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(concatenated_iterable)) # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Discussion:
Be the first person to comment on this question !
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers