How can I understan the "for" here?

James Mills prologic at shortcircuit.net.au
Thu Dec 11 09:51:31 EST 2008


On Fri, Dec 12, 2008 at 12:44 AM, Kermit Mei <kermit.mei at gmail.com> wrote:
> I can't understand the second sentence because of the "for ... in".
> I consider that the syntactics of "for" should be:
>
> for k,v in params.items():
>   ......
>
> But there's no a colon here, why it can work?

It's called a list comprehension.
The "for x in y" construct becomes an expression.

Simple example:

Build a list of integers divisible by 3:

>>> [x for x in xrange(10) if not x % 3]
[0, 3, 6, 9]


cheers
James

-- 
--
-- "Problems are solved by method"



More information about the Python-list mailing list