No, any method will require the resolution of each result. You can do
iter_length = len(list(iterable))
but doing this on an infinite iterator will certainly never return. It will also consume an iterator, and it will have to reset if you want to use the contents.
Telling us what the real problem you are trying to solve can help us find the best way to reach your actual goal.
Edit: with list() entire iteration will be read into memory immediately, which may be undesirable. Another way is to do
sum(1 for _ in iterable)
like another person. This will avoid storing it in memory.
Daenyth Jul 27 '10 at 16:34 2010-07-27 16:34
source share