suppose i have the following code:
>>> a = re.finditer("(?P<char>\w)","hello world")
>>> for i in a:
print i.groupdict()
{'char': 'h'}
{'char': 'e'}
{'char': 'l'}
{'char': 'l'}
{'char': 'o'}
{'char': 'w'}
{'char': 'o'}
{'char': 'r'}
{'char': 'l'}
{'char': 'd'}
as you can see, there are results and everything is great! however, i want to be able to know the len(a) so i would do:
>>> len(a)
Traceback (most recent call last):
File "<pyshell#79>", line 1, in <module>
len(a)
TypeError: object of type 'callable-iterator' has no len()
my question is, how can i get the length of a, or more generally, how do i get the length of callable-iterator so i can know if there were any results before i parse them.
You need to turn the iterable into a list to get the length:
Note that this consumes the iterable, so you may not be able to iterate over it again. Store the
list(a)result in an intermediary before callinglen()on it to be able to reuse all the elements in the list still: