'For' loop symmetry with list comprehensions.

Peter Hansen peter at engcorp.com
Thu Jul 3 12:28:40 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
> 
> Naturally this is the same as:
> 
> for x in words:
>     if x.startswith('foo'):
>         print x

My brain tends to parse the one-line statement form of the syntax
(which works so nicely in the list-comprehension expression) in a 
different way:

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

looks more like it is just another way to say:

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

Besides, hasn't everyone got the idea lately that it should require
a *very strong* benefit to even begin to consider discussion another
change in Python syntax?  

Why do people keep suggesting what amount to trivial tweaks which 
will _never_ pay back the effort that would have to go into the 
discussion and the changes to the compiler, the documentation, and 
everything else, before this would ever be accepted?

-Peter




More information about the Python-list mailing list