list comprehension

Eric S. Johansson esj at harvee.org
Fri Jun 30 08:11:03 EDT 2006


tac-tics wrote:
> a wrote:
>> can someone tell me how to use them
>> thanks
> 
> sigh...
> You do a google on them:
> 
> http://docs.python.org/tut/node7.html#SECTION007140000000000000000
> 

thank you for the reminder.  But after reading it, I was left with a 
question.  Why the new syntax for what appears to be nothing more than a 
different way of expressing nested for loops?  at first I tried 
rearranging the one of the examples from the previously referenced 
document to clarify (in my mind) what was going on:

[x*y
     for x in vec1
         for y in vec2
]

then when I tried to rewrite it as a nested for loop, that's when I 
realized list comprehensions is a method of returning the calculation in 
the interior of a for loop as an assignment without requiring 
aggregation of results in a temporary variable.

so a double thank you to you because you've clarified something that has 
been a mystery to me for awhile.

now if I could find someone to explain distutils and eggs so that I 
understand that as well... ;-)  Heck, I would even settle for a wizard 
tool generating the first approximation for me.

---eric




More information about the Python-list mailing list