'For' loop symmetry with list comprehensions.

Hannu Kankaanpää hanzspam at yahoo.com.au
Wed Jul 2 14:36:37 EDT 2003


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.




More information about the Python-list mailing list