[Python-3000] List-Comp style for loops?

Crutcher Dunnavant crutcher at gmail.com
Sat Apr 8 05:55:55 CEST 2006


I'm not really sure what that guy is talking about.

Sorry,

On 4/7/06, Guido van Rossum <guido at python.org> wrote:
> On 4/7/06, Crutcher Dunnavant <crutcher at gmail.com> wrote:
> > Basic idea:
> >
> > [for ... in ...]+ if ...:
> >   body
> >
> > A) it should be syntatically simple to parse.
> > B) it establishes a nice symetry with list comprehensions.
>
> Are you in cahoots with the person who posted the first response to my
> Artima blog on today? :-)
> (http://www.artima.com/forums/flat.jsp?forum=106&thread=155514)
>
> I can't make heads or tails of this. What is it supposed to do? What
> is it supposed to replace?

I'm not really sure what that guy is talking about.

When list comps are explained, they are often described as being similar to:

L = []
for i in ...:
  for j in ...:
    for k in ...:
      if ...:
         L.append(somefunof(i,j,k))

Well, if it is nice to have these constructs when we want the list, it
will be nice to have them when we _don't_ care about the list. I find
myself occasionally using listcomps to build looping constructs just
cause they're nice, without saving the list, but of course, this is
inefficient. So what I'm asking for is that this (without anything
getting the comp value, and in hackish BNF):

  [ EXPR (for VARSEXPR in SEQ)+ [if TEST]]

be equivalent to:

  (for VARSEXPR in SEQ)+ [if TEST]:
    EXPR

So, for example:

  [ x(i) for i in range(n) if y(i) ]

Could be represented as:

  for i in range(n) if y(i):
    x(i)

--
Crutcher Dunnavant <crutcher at gmail.com>
littlelanguages.com
monket.samedi-studios.com


More information about the Python-3000 mailing list