'For' loop symmetry with list comprehensions.

Manish Jethani manish.j at gmx.net
Wed Jul 2 19:15:19 EDT 2003


Hannu Kankaanpää wrote:
> One can currently say this with list comprehensions:
> 
> [x.lower() for x in words if x.startswith('foo')]
> 
> Wouldn't it be better if the normal 'for' syntax was symmetrical
> with the notation used in list comprehensions? To be more specific,
> it lacks the 'if' part. I.e. this should be possible:
> 
> for x in words if x.startswith('foo'):
>     print x

To some extent, yes:

for x in words:
    if x.startswith('foo'): print x

You saved one level of indent.

-Manish

-- 
Manish Jethani (manish.j at gmx.net)
phone (work) +91-80-51073488





More information about the Python-list mailing list