'For' loop symmetry with list comprehensions.

Michele Simionato mis6 at pitt.edu
Thu Jul 3 11:46:10 EDT 2003


hanzspam at yahoo.com.au (Hannu Kankaanpää) wrote in message news:<840592e1.0307021036.508d7d7d at posting.google.com>...
> 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
> 
> Naturally this is the same as:
> 
> for x in words:
>     if x.startswith('foo'):
>         print x
> 
> So one only benefits from one level shorter tabulation in
> some situations. I admit this isn't much of a benefit in terms
> of making shorter programs, but it would make the language
> more symmetrical, which I do consider a win. It's a feature
> that wouldn't need any learning or remembering since it's already
> used in list comprehensions.

+1

                        Michele




More information about the Python-list mailing list