Pythonic way to sum n-th list element?
David Eppstein
eppstein at ics.uci.edu
Fri Apr 18 19:01:47 EDT 2003
In article <C8_na.646$dP1.2863 at newsc.telia.net>,
"Tim Gahnström /Bladerman" <tim at bladerman.com> wrote:
> > What's wrong with:
> >
> > t = 0
> > for y in x:
> > t += y[1]
> >
> > The extra variable? The number of lines?
>
> I am really curious about that to, I would most definitley say that this is
> the most pythonic way. It is simple and easily readabel by anyone and I am
> sure it is just as fast any of the other way.
If you want the sum of a list of items, you should write it in a way
that looks like "the sum of a list of items", not in a way that looks
like "loop over these items, maintain another variable t, perform a
sequence of additions". Why do we have high level languages if not to
express our intentions at a higher level and let the language worry
about what low-level operations are needed to implement it?
--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
More information about the Python-list
mailing list