0

I want to figure out how to return generator outputs. I know that next(generator) returns the yielded output of the function, but I want to know how to return multiple yielded outputs.

For instance:

alist = [1,2,3,4,5,6]
def aiterator():
    for i in alist:
        yield i+1

generator = aiterator()

I know that next(generator) will return 1, but how do I return 2,3, or if I want, 2,3,4 without typing next(generator) twice or three times?

Specifically, I'm thinking of another function which will return the number of next(generator)s:

for i in aiterator():
  return ?
halo09876
  • 2,725
  • 12
  • 51
  • 71
  • generator always return each yield one by one with next tick, So if you want different output then you need to change yield value. – Biplab Malakar Nov 10 '18 at 03:25

1 Answers1

-1

If you want to output the whole list one by one, use:

while True:
  next(generator)

If you want more customized output, then as mentioned in one of the comments, change the yield value.

sla3k
  • 209
  • 1
  • 4
  • After the generator is exhausted, the `while` loop will break with `StopIteration` error. – sla3k Nov 10 '18 at 03:48