count items in generator

Cameron Laird claird at lairds.us
Sun May 14 12:31:25 EDT 2006


In article <1hfarom.1lfetjc18leddeN%aleax at mac.com>,
Alex Martelli <aleax at mac.com> wrote:
			.
			.
			.
>My preference would be (with the original definition for
>words_of_the_file) to code
>
>   numwords = sum(1 for w in words_of_the_file(thefilepath))
			.
			.
			.
There are times when 

    numwords = len(list(words_of_the_file(thefilepath))

will be advantageous.

For that matter, would it be an advantage for len() to operate
on iterables?  It could be faster and thriftier on memory than
either of the above, and my first impression is that it's 
sufficiently natural not to offend those of suspicious of
language bloat.



More information about the Python-list mailing list