conditional for-statement

seb sdementen at gmail.com
Tue Aug 25 13:25:27 EDT 2009


On Aug 23, 11:02 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Sun, Aug 23, 2009 at 1: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 ?
>
> It's not been added since it's completely unnecessary (see the several
> alternatives already presented by others).
> There have been a few other mailinglist threads on adding essentially
> the same syntax. None have proved fruitful.
>
> Cheers,
> Chris
> --http://blog.rebertia.com- Hide quoted text -
>
> - Show quoted text -

On Aug 23, 11:02 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Sun, Aug 23, 2009 at 1: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 ?
>
> It's not been added since it's completely unnecessary (see the several
> alternatives already presented by others).
> There have been a few other mailinglist threads on adding essentially
> the same syntax. None have proved fruitful.
>
> Cheers,
> Chris
> --http://blog.rebertia.com- Hide quoted text -
>
> - Show quoted text -

Tx Chris for your reply !

i am still a bit puzzle by the following.

I read in http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Generators

"""Python 3.0 unifies all collection types by introducing dict and set
comprehensions, similar to list comprehensions:

>>> [ n*n for n in range(5) ] # regular list comprehension
[0, 1, 4, 9, 16]
>>>
>>> { n*n for n in range(5) } # set comprehension
{0, 1, 4, 16, 9}
>>>
>>> { n: n*n for n in range(5) } # dict comprehension
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
"""
and we can add to this list the quite similar syntax for generator
expressions.

On all these loop constructs, one can consistenly add filtering on a
condition by adding an "if ..." after the "for ... in ..." part (and
it looks to me difficult to argue, for instance, that we should not
allow filtering for dict comprehesion because we could get the same
result by some other construct)

If the "if ..." part after the "for ... in ..." is so much used in all
these list/dict/set comprehensions and in the generator expressions,
it makes sense to me to have it also for the "for as a statement"
syntax :
[ n*n for n in range(10) if n%3 == 0]
{ n*n for n in range(10) if n%3 == 0}
{ n: n*n for n in range(10) if n%3 == 0}
( n*n for n in range(10) if n%3 == 0)
for n in range(10) if n%3 == 0:
  print n*n

In fact, we often see the list comprehension [ n*n for n in range(10)
if n%3 == 0] explained as being equivalent to
l = []
for n in range(10):
  if n%3 == 0:
    l.append(n)

We could as consistenly explain that the syntax

for n in range(10) if n%3==0:
  body

means

for n in range(10):
  if n%3==0:
    body

This syntax has also the benefit of avoiding an extra level of
indentation (the one for the if) that bears no real meaning on a
structural level.

Maybe a PEP could do the job...

Sébastien



More information about the Python-list mailing list