Python Programming - Generators - Discussion

Discussion Forum : Generators - General Questions (Q.No. 99)
99.
What does the yield from statement do when used in a generator?
It yields a single value from the generator
It raises a TypeError
It delegates part of the generator's operation to another generator or iterable
It stops the generator and raises a StopIteration exception
Answer: Option
Explanation:
yield from is used to delegate part of the generator's operation to another generator or iterable. It simplifies the syntax for generators that yield from another iterable.
def generator_delegator():
    yield from range(3)
    yield from 'abc'

result = list(generator_delegator())
print(result)  # Output: [0, 1, 2, 'a', 'b', 'c']
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.