[Tutor] List comprehension

Sean 'Shaleh' Perry shalehperry@attbi.com
Fri Apr 11 00:53:01 2003

On Thursday 10 April 2003 20:50, Alfred Milgrom wrote:
> Hi Nano:
> Danny Yoo has already answered your question in January this year.
> You can find Danny's answer at
> http://aspn.activestate.com/ASPN/Mail/Message/python-Tutor/1503341
> In short, the way to do it is to use the reduce() function:
> import operator
> myList = [1,2,3,4,5,6]
> sum = reduce(operator.add, myList)

danny did a good job of explaining reduce.

How about the list comprehension anyways?  A list comprehension does not work 
here because only expressions are allowed and assignment is not an 
expression.  Beyond that, a list comprehension creates a new list.  In this 
case, the extra list is not needed and the time and memory it uses would be