How to find the length of an iterator?

Diez B. Roggisch deetsNOSPAM at web.de
Thu Apr 29 09:01:38 EDT 2004


Thorsten Kampe wrote:

> What is the best way to find out the length of an iterator? len()
> doesn't work. In the above code snippet[1] I am only interested in the
> index (= length of the iterator), so I'm using len(list(iterator))...
> 
> Anything better?

Its the nature of an iterator that it doesn't support anything beyond access
to the current item and signaling when its finished. So if you need the
length, try to get a hold on the underlying connection.

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list