conditional for-statement

seb sdementen at gmail.com
Sun Aug 23 16:58:01 EDT 2009


On Aug 23, 10:36 pm, seb <sdemen... at gmail.com> wrote:
> 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 ?- Hide quoted text -
>
> - Show quoted text -

I just found the same thread on the python ideas group at
http://mail.python.org/pipermail/python-ideas/2009-April/004278.html

sorry for reposting



More information about the Python-list mailing list