2009/6/19 John Graham
Exactly how does itertools.tee not cover this use case?
I've been wondering the same sort of thing. But if you're going to sequentially run through the iterator twice for i in it: do something for i in it: do something else then itertools.tee is documented as being basically equivalent to, but worse than, list(): "In general, if one iterator is going to use most or all of the data before the other iterator, it is faster to use list() instead of tee()". So the question becomes, how does list() not cover this use case. The only answer I can see is that the proposal avoids allocating temporary storage for all of the values generated. Consider xrange(1000000) - if you can call xrange twice, rather than saving a million values, you've saved a lot of memory. But this only applies when you're writing functions designed to take extremely general iterators. Someone writing a function which can take essentially arbitrary iterators, and which has to handle unboundedly large input gracefully, probably has worse problems than the lack of standard library support for something they can code themselves fairly easily... Paul.