"For" loop and list comprehension similarity

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • s.lipnevich@gmail.com

    #1

    "For" loop and list comprehension similarity

    Hi All,

    I apologize if this was brought up before, I couldn't find any "prior
    art" :-).
    On more than one occasion, I found myself wanting to use a "conditiona l
    loop" like this (with "Invalid syntax" error, of course):

    for i in c if <test>:
    print i*2

    ....because it's similar to the list comprehension construct:

    [i*2 for i in c if <test>]
    ---------

    Is this the intended difference in constructs? The available equivalent
    feels a bit awkward:

    for i in c:
    if <test>:
    print i*2

    Just curious. Thanks!

    Sergey.

  • Mitja Trampus

    #2
    Re: &quot;For&qu ot; loop and list comprehension similarity

    s.lipnevich@gma il.com wrote:
    [color=blue]
    > On more than one occasion, I found myself wanting to use a "conditiona l
    > loop" like this (with "Invalid syntax" error, of course):
    >
    > for i in c if <test>:
    > print i*2[/color]

    Maybe there's been a PEP, don't really know...
    Currently, the only sensible alternative is what you've written below:
    [color=blue]
    > The available equivalent
    > feels a bit awkward:
    >
    > for i in c:
    > if <test>:
    > print i*2[/color]


    This indeed doesn't look nice, especially if you've got lots of code instead of just
    print. An alternative which avoids double indentation is

    for i in c:
    if not <test>: continue
    print i*2

    Comment

    • s.lipnevich@gmail.com

      #3
      Re: &quot;For&qu ot; loop and list comprehension similarity

      Thank you for replying, Mitja! That *is* a nice alternative.

      Do you think it's a good idea to ask on comp.python.dev el if they would
      be interested in a PEP about this (provided there is none)?

      Cheers,
      Sergey.

      Comment

      • Grant Edwards

        #4
        Re: &quot;For&qu ot; loop and list comprehension similarity

        On 2006-03-26, s.lipnevich@gma il.com <s.lipnevich@gm ail.com> wrote:[color=blue]
        > Hi All,
        >
        > I apologize if this was brought up before, I couldn't find any "prior
        > art" :-).
        > On more than one occasion, I found myself wanting to use a "conditiona l
        > loop" like this (with "Invalid syntax" error, of course):
        >
        > for i in c if <test>:
        > print i*2
        >
        > ...because it's similar to the list comprehension construct:
        >
        > [i*2 for i in c if <test>]
        > ---------
        >
        > Is this the intended difference in constructs? The available equivalent
        > feels a bit awkward:
        >
        > for i in c:
        > if <test>:
        > print i*2[/color]

        for j in [i*2 for i in c if <test>]:
        print j

        --
        Grant Edwards grante Yow! .. I wonder if I
        at ought to tell them about my
        visi.com PREVIOUS LIFE as a COMPLETE
        STRANGER?

        Comment

        • Ben Finney

          #5
          Re: &quot;For&qu ot; loop and list comprehension similarity

          s.lipnevich@gma il.com writes:
          [color=blue]
          > On more than one occasion, I found myself wanting to use a "conditiona l
          > loop" like this (with "Invalid syntax" error, of course):
          >
          > for i in c if <test>:
          > print i*2
          >
          > ...because it's similar to the list comprehension construct:
          >
          > [i*2 for i in c if <test>][/color]

          Why not combine the two:

          for i in [j for j in c if <test>]:
          print i*2

          --
          \ "I got food poisoning today. I don't know when I'll use it." |
          `\ -- Steven Wright |
          _o__) |
          Ben Finney

          Comment

          • s.lipnevich@gmail.com

            #6
            Re: &quot;For&qu ot; loop and list comprehension similarity

            > Why not combine the two:

            I guess because (at least in source code) you're doing a loop twice
            :-). I don't know what a compiler would do. I think though that the
            "for i in c if test:" construct is more readable and maybe can even be
            better optimized.
            Thanks!

            Sergey.

            Comment

            • John Zenger

              #7
              Re: &quot;For&qu ot; loop and list comprehension similarity

              Rather than a list comprehension, it would be faster and more
              memory-efficient to use a generator comprehension. Just change the
              square brackets to parentheses:

              for j in (i*2 for i in c if <test>):
              print j


              Grant Edwards wrote:[color=blue]
              > On 2006-03-26, s.lipnevich@gma il.com <s.lipnevich@gm ail.com> wrote:
              >[color=green]
              >>Hi All,
              >>
              >>I apologize if this was brought up before, I couldn't find any "prior
              >>art" :-).
              >>On more than one occasion, I found myself wanting to use a "conditiona l
              >>loop" like this (with "Invalid syntax" error, of course):
              >>
              >> for i in c if <test>:
              >> print i*2
              >>
              >>...because it's similar to the list comprehension construct:
              >>
              >> [i*2 for i in c if <test>]
              >> ---------
              >>
              >>Is this the intended difference in constructs? The available equivalent
              >>feels a bit awkward:
              >>
              >> for i in c:
              >> if <test>:
              >> print i*2[/color]
              >
              >
              > for j in [i*2 for i in c if <test>]:
              > print j
              >[/color]

              Comment

              • Terry Reedy

                #8
                Re: &quot;For&qu ot; loop and list comprehension similarity


                <s.lipnevich@gm ail.com> wrote in message
                news:1143414870 .330921.305890@ j33g2000cwa.goo glegroups.com.. .[color=blue][color=green]
                >> Why not combine the two:[/color]
                >
                > I guess because (at least in source code) you're doing a loop twice
                > :-). I don't know what a compiler would do. I think though that the
                > "for i in c if test:" construct is more readable and maybe can even be
                > better optimized.[/color]

                There are also the filter and ifilter functions:

                for i in filter(testfunc , c):

                tjr



                Comment

                • s.lipnevich@gmail.com

                  #9
                  Re: &quot;For&qu ot; loop and list comprehension similarity

                  I think I like generator comprehension in this case better than either
                  list comprehension or a filter because both of the latter create a new
                  full "result list" before the loop even begins. At least I suppose they
                  do. Also, I think Mitja's suggestion "if not <test>: continue" and
                  Terry's filter function are more readable than comprehensions.
                  It's not a contest though :-), all these variants are great, thank you
                  all!
                  Do you think this discussion is a proof that the following principle
                  got violated, or do you think that "loop with condition" is not such an
                  atomic thing to be subject to this: "There should be one -- and
                  preferably only one -- obvious way to do it."
                  Cheers,

                  Sergey.

                  Comment

                  • Peter Hansen

                    #10
                    Re: &quot;For&qu ot; loop and list comprehension similarity

                    s.lipnevich@gma il.com wrote:[color=blue]
                    > Do you think this discussion is a proof that the following principle
                    > got violated, or do you think that "loop with condition" is not such an
                    > atomic thing to be subject to this: "There should be one -- and
                    > preferably only one -- obvious way to do it."[/color]

                    Mitja's suggestion was the one obvious way. The others are all
                    interesting, maybe even preferable in some cases, but I don't think most
                    experienced Python programmers would be more likely to start with one of
                    them than with the simple for-loop-with-explicit-test.

                    -Peter

                    Comment

                    • Terry Reedy

                      #11
                      Re: &quot;For&qu ot; loop and list comprehension similarity


                      "Peter Hansen" <peter@engcorp. com> wrote in message
                      news:e08r7a$4b5 $1@sea.gmane.or g...[color=blue]
                      > s.lipnevich@gma il.com wrote:[color=green]
                      >> Do you think this discussion is a proof that the following principle
                      >> got violated, or do you think that "loop with condition" is not such an
                      >> atomic thing to be subject to this: "There should be one -- and
                      >> preferably only one -- obvious way to do it."[/color]
                      >
                      > Mitja's suggestion was the one obvious way. The others are all
                      > interesting, maybe even preferable in some cases, but I don't think most
                      > experienced Python programmers would be more likely to start with one of
                      > them than with the simple for-loop-with-explicit-test.[/color]

                      If by 'explicit-test' you mean a nested if-statement, then I agree. When I
                      mentioned filter() as one way to avoid the obvious, I was aware that it
                      creates an intermediate list that is usually not needed. (And if it is
                      needed, then it should be name-assigned before the loop.)

                      Terry Jan Reedy



                      Comment

                      Working...