Iterator length

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Thu Jan 18 19:55:39 EST 2007


George Sakkis:
> Is this a rhetorical question ? If not, try this:

It wasn't a rhetorical question.


> >>> x = (i for i in xrange(100) if i&1)
> >>> if leniter(x): print x.next()

What's your point? Maybe you mean that it consumes the given iterator?
I am aware of that, it's written in the function docstring too. But
sometimes you don't need the elements of a given iterator, you just
need to know how many elements it has. A very simple example:

s = "aaabbbbbaabbbbbb"
from itertools import groupby
print [(h,leniter(g)) for h,g in groupby(s)]

Bye,
bearophile




More information about the Python-list mailing list