Pythonic way to sum n-th list element?

Paul Rubin http
Fri Apr 18 16:28:58 EDT 2003


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])




More information about the Python-list mailing list