Pythonic way to sum n-th list element?

Donn Cave donn at u.washington.edu
Sat Apr 19 01:45:37 CEST 2003


Quoth David Eppstein <eppstein at ics.uci.edu>:
| 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?

So you see the functional programming answers as "higher level"
than the imperative, procedural approach taken above?  I think
many functional programming enthusiasts would see it that way,
but it's more interesting to hear it from a Python programmer.

I personally don't see it - I mean, I think there are some good
things about the functional approach, but I guess I have higher
standards for higher level.

	Donn Cave, donn at u.washington.edu




More information about the Python-list mailing list