[Tutor] list comprehensions

Christer Edwards christer.edwards at gmail.com
Tue Oct 6 21:57:16 CEST 2009

I've been studying python now for a few weeks and I've recently come
into list comprehensions. Some of the examples that I've found make
sense, and I find them readable and concise. In particular I'm
referring to the python docs on the topic
Those make sense to me. The way I understand them is:

do something to x for each x in list, with an optional qualifier.

On the other hand I've seen a few examples that look similar, but
there is no action done to the first x, which confuses me. An example:

print sum(x for x in xrange(1,1000) if x%3==0 or x%5==0)

In this case is sum() acting as the "action" on the first x? Can
anyone shed some light on what it is I'm not quite grasping between
the two?


More information about the Tutor mailing list