[Tutor] List comprehension

Alfred Milgrom fredm@smartypantsco.com
Thu Apr 10 23:53:02 2003


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)

All the best,
Fred Milgrom

At 10:12 AM 11/04/03 +0700, Nova Sano S. Surbakti wrote:
>Dear List-ers,
>
>How can I simplify this lines:
>         > sum = 0
>         > myList = [1,2,3,4,5,6]
>         > for each in myList:
>         >       sum += each
>
>Into shorter, more elegant line(s)?
>
>TFYH,
>Nano
>--Peace!--