Answer the question
In order to leave comments, you need to log in
How to find out the length of an iterator?
To find out the length of a list, use len(). But len() does not work when I make an iterator from the list using iter().
Is there any way to find out the length of an iterator? If so, how?
a = [1,2,3,4]
print(len(a))
a2 = iter(a)
print(len(a2))
Answer the question
In order to leave comments, you need to log in
a = [1,2,3,4]
print(len(a))
a2 = iter(a)
print(len(list(a2)))
a = [1,2,3,4]
print(len(a))
a2 = iter(a)
print(sum(1 for _ in a2))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question