yet another list compr. suggestion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • palo

    #1

    yet another list compr. suggestion

    I appologize for suggesting (though very humbly) a syntax extension
    (maybe particularly idiotic) but just as a wild fantasy, what would you
    think about this:
    as a substitute for filter, people often use
    [x for x in y if z(x)]
    The suggestion is that the same result could be achieved by
    [x in y if z(x)]

  • Alan Franzoni

    #2
    Re: yet another list compr. suggestion

    palo on comp.lang.pytho n said:
    [color=blue]
    > The suggestion is that the same result could be achieved by
    > [x in y if z(x)][/color]

    It's just a special case... and it saves very few carachters... I don't
    think it would justify an update to the parser. What if you want to do
    something like:

    [str(x) for x in y if z(x)]

    You've got to stick to the present syntax.

    --
    Alan Franzoni <alan.franzoni. xyz@gmail.com>
    -
    Togli .xyz dalla mia email per contattarmi.
    Rremove .xyz from my address in order to contact me.
    -
    GPG Key Fingerprint:
    5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E

    Comment

    • palo

      #3
      Re: yet another list compr. suggestion

      you'd save more characters in
      [not_very_conven ient_name for not_very_conven ient_name in y if z]
      (just kidding)

      Comment

      • palo

        #4
        Re: yet another list compr. suggestion

        you'd save more in

        [not_very_conven ient_name for not_very_conven ient_name in y if z]

        (just kidding)

        Comment

        • Christoph Zwerschke

          #5
          Re: yet another list compr. suggestion

          palo wrote:[color=blue]
          > ... what would you think about this:
          > as a substitute for filter, people often use
          > [x for x in y if z(x)]
          > The suggestion is that the same result could be achieved by
          > [x in y if z(x)][/color]

          In the original syntax, if z(x) is always true,
          you can leave the "if z(x)" part away, which is intuitive.
          But in your syntax, the "if z(x)" is essential.
          If you leave it away, you get a completely different
          construct, a list with a bool value as element.
          You would not expect that an "if" keyword indicates a loop.

          I think the "for" is absolutely essential to recognize
          a list comprehension, not only for the parser, but also
          for the human reader. It's already confusing enough.
          Just compare the following:

          [x in y] -> list with one element
          [x in y if z(x)] -> list comprehension (in your syntax)
          [x in y if z(x) else 0] -> list with one element (Py 2.5)

          -- Christoph

          Comment

          Working...