[Python-ideas] itertools.documentation.ncycles is a bit of a sore thumb
jh at improva.dk
Thu Aug 4 01:16:14 CEST 2011
On 2011-08-03 23:22, Raymond Hettinger wrote:
> On Aug 3, 2011, at 2:41 PM, Jacob Holm wrote:
>> How about using this alternate recipe then:
>> def ncycles(iterable, n):
>> return chain.from_iterable(tee(iterable, n))
> Really? Think about what that does for a large value of n.
It predictably creates an n-tuple of small objects (the cpython "tee"
implementation is really quite efficient), and a deque for storing the
values from the iterable as it gets them. For small values of n and
unknown (potentially large/expensive) iterables this is still an
improvement over the recipe in the documentation if you stop iterating
before reaching the end of the first repetition.
The other recipe I mentioned was specifically designed to handle large n
Both my recipes have the nice property requested by the OP that they
don't compute values until they are needed. This is relevant if the
values are expensive to compute and there is a chance that you might not
need them all. In other words, exactly the case where you are most
likely to want to use itertools.
For iterables with say 50 items or less that are cheap to compute,
and/or where you know that you will exhaust the iterable anyway, and
where you don't mind "looking ahead", the current recipe in the
itertools documentation is just about perfect.
More information about the Python-ideas