Pythonic way to sum n-th list element?

Jack Diederich jack at performancedrivers.com
Fri Apr 18 17:19:15 EDT 2003


> > import operator
> > reduce (operator.add, [y[1] for y in x])
> 
> 
> Aha!  That's what I was looking for.  Thanks...
> 

Actually he said
  reduce(operator.add, [y[1] for y in x], 0)
                                        ^^^^
reduce without the third argument will raise an exception if x is empty,
so it is best to get in the habit of including one.

-jack





More information about the Python-list mailing list