[Python-ideas] iterator length

Paul Moore p.f.moore at gmail.com
Mon Aug 9 21:52:39 CEST 2010

On 9 August 2010 18:16, Alexandru Moșoi <brtzsnr at gmail.com> wrote:
> 1) sum(1 for e in iterator)
> 2) len(list(iterator))
> First solution is slow, the second solution uses O(N) extra memory.
> I propose the addition of a new function ilen() which is functionally
> equivalent to:
> def ilen(iterator):
>  return sum(1 for e in iterator)

You say that this solution "is slow" and then you propose it? I'm confused.

Besides which, as you define it, it exhausts the iterator, which makes
it useless. It may be useful for an *iterable*, but most of them
support len in any case.

> This function should be different from len() because it's time
> complexity is O(N) (most people assume that len() takes O(1)) and it
> consumes the iterator.

Precisely. So how is it useful?

If you could show some real code that uses your ilen function, that
would help clarify. But it still won't explain why the function should
be built in rather than just defined by your code where it's needed -
you'll have to have some very common and compelling use cases to argue


More information about the Python-ideas mailing list