How to find the length of an iterator?

Thorsten Kampe thorsten at thorstenkampe.de
Thu Apr 29 08:38:37 EDT 2004


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:]]



More information about the Python-list mailing list