Ideas about optional filter on for loop statements

Konstantin Veretennicov kveretennicov at yahoo.com
Wed May 12 17:35:11 EDT 2004


bluczkie at andrew.cmu.edu (Brian L.) wrote in message news:<c186c44d.0405110926.5eead044 at posting.google.com>...

> The strongest argument I can think of against it is that for loop
> statements should be reserved for iteration only, and that they
> shouldn't have functional features (for instance, the for loop can not
> and will not ever have 'map' like semantics, so why should it have
> filter?). I don't think, however that this is a problem, and
> considering the number of times that I've written loops in the
> following patterns:
> 
> for x in l:
>     if x is None: continue
>     ...
> 
> or
> 
> for x in l:
>     if x is not None:
>         ... 
> 
> I think that a more pythonic syntax may be in order.

Remember how many times you've written something like this:

if condition:
    return

Wouldn't it be great to have a single-line "return if condition"? ;)
But what about "... preferably only one ... obvious way to do it"?

> 
> for x in l if x is not None:
>     ...
> 
> is certainly cleaner and easier to read.

Not sure about that. Sometimes i break list comprehensions
into several lines to make them more readable.

- kv



More information about the Python-list mailing list