conditional for-statement

seb sdementen at gmail.com
Sun Aug 23 16:36:10 EDT 2009


On Aug 23, 6:18 pm, John Posner <jjpos... at optimum.net> wrote:
> >> Hi,
>
> >> i was wondering if there is a syntax alike:
>
> >> for i in range(10) if i > 5:
> >>     print i
>
> > You can write
>
> > for i in filter(lambda i: i > 5, range(10)):
> >     print i
>
> > but
>
> > for i in range(10):
> >     if i > 5:
> >         print i
>
> > it' better readable, and
>
> > for i in range(6,10):
> >     print i
>
> > it's event better.
>
> How about using a generator expression instead of a list?
>
>   for i in (x for x in range(10) if x > 5):
>       print i
>
> -John

Indeed, but we could have the same syntax than for generators but
directly in the for statement as in
for variable in generator if condition:
    body

Is there a special reason for not doing so ? A rejected PEP ?



More information about the Python-list mailing list