Pythonic way to sum n-th list element?

Guy Middleton guy at obstruction-no-spam.com
Fri Apr 18 16:41:11 EDT 2003


In article <7x4r4vy2gl.fsf at ruckus.brouhaha.com>,
Paul Rubin  <http://phr.cx@NOSPAM.invalid> wrote:
> guy at obstruction-no-spam.com (Guy Middleton) writes:
> > What is the most Pythonic way to sum the n-th element of a list of lists (or
> > of tuples)?
> > 
> > The obvious (to me) way is to use reduce, a list comprehension, and a lambda:
> > 	... 
> > 	>>> reduce(lambda a, b: a+b, [y[1] for y in x])
> > 	6
> > 	>>>
> > 	$
> > 
> > But I don't really like lambda, is there a better way to do this?
> 
> import operator
> reduce (operator.add, [y[1] for y in x])


Aha!  That's what I was looking for.  Thanks...

 -Guy




More information about the Python-list mailing list