Yield only as many are required from a generator
Posted
by Matt Joiner
on Stack Overflow
See other posts from Stack Overflow
or by Matt Joiner
Published on 2010-06-18T13:53:37Z
Indexed on
2010/06/18
14:03 UTC
Read the original article
Hit count: 322
I wish to yield from a generator only as many items are required.
In the following code
a, b, c = itertools.count()
I receive this exception:
ValueError: too many values to unpack
I've seen several related questions, however I have zero interest in the remaining items from the generator, I only wish to receive as many as I ask for, without providing that quantity in advance.
It seems to me that Python determines the number of items you want, but then proceeds to try to read and store more than that number.
How can I yield only as many items as I require, without passing in how many items I want?
© Stack Overflow or respective owner