How do we yield from another sub-generator, but with transformation/processing?
for example: in code below, main_gen yields x after transformation using f(x)
def f(x):
return 2*x
def main_gen():
for x in sub_gen():
yield f(x)
can this be replaced with yield from and if so how?
def main_gen():
yield from ***
You could do:
def main_gen():
yield from map(f, sub_gen())
But then, why not:
def main_gen():
return map(f, sub_gen())
Which is a lazy iterator anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With