By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

How to find the length of an iterator?

P: n/a
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?

Thanks, Thorsten

[2]
def part(seq, func):
from itertools import takewhile
index = len(list(takewhile(func, seq)))
return [seq[:index], seq[index:]]
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
Diez B. Roggisch wrote:
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.


s/connection/collection/

Got confused on my new touchstream dvorak keyboard :)
--
Regards,

Diez B. Roggisch
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.