[Baypiggies] len(iterable)

Guido van Rossum guido at python.org
Wed Aug 6 22:08:30 CEST 2008

sum(1 for x in it)

On Wed, Aug 6, 2008 at 1:01 PM, Shannon -jj Behrens <jjinux at gmail.com> wrote:
> I have an iterable, and I want to find out how long it is.  I don't
> care if it consumes the iterable.  Furthermore, I know the iterable is
> not infinite.  Consider:
>>>> len((i for i in xrange(10)))
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: object of type 'generator' has no len()
> It makes sense that len() not be defined, but I can't seem to find a
> simple count function.  Of course, I can write one, but it seems like
> it should exist in itertools or something.  (By the way, the count
> function in itertools is something completely different.)

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Baypiggies mailing list