a few extensions for the itertools

Mathias Panzenboeck e0427417 at student.tuwien.ac.at
Mon Nov 20 10:43:08 EST 2006


Duncan Booth wrote:
> Mathias Panzenboeck <e0427417 at student.tuwien.ac.at> wrote:
> 
>> No, because the builtin sum want's a list. This can also handle any
>> kind of iterable, so this would work:
>>
>> isum(i**2 for i in xrange(100))
>>
>> sum would need firs the whole list to be generated:
>>
>> sum([i**2 for i in xrange(100)])
> 
> Really?
> 
>>>> sum(i**2 for i in xrange(20000000))
> 2666666466666670000000L
> 
> seems to work fine, and judging by the memory usage it pretty obviously 
> doesn't create an intermediate list.

Very strange. I must have made some strange error. I tried that (more than once) and it failed. 
Don't know why. But now it works here, too. What did I write, when it failed?



More information about the Python-list mailing list