set/dict comp in Py2.6

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bearophileHUGS@lycos.com

    set/dict comp in Py2.6

    I'd like to know why Python 2.6 doesn't have the syntax to create sets/
    dicts of Python 3.0, like:

    {x*x for x in xrange(10)}
    {x:x*x for x in xrange(10)}

    Bye,
    bearophile
  • Steven D'Aprano

    #2
    Re: set/dict comp in Py2.6

    On Sat, 25 Oct 2008 01:13:08 -0700, bearophileHUGS wrote:
    I'd like to know why Python 2.6 doesn't have the syntax to create sets/
    dicts of Python 3.0, like:
    >
    {x*x for x in xrange(10)}
    {x:x*x for x in xrange(10)}
    Maybe nobody asked for it?

    Personally, I don't see the advantage of set and dict comprehensions. I
    think the value of them is very marginal, not worth the additional syntax.

    set([x*x for x in xrange(10)])
    dict((x, x*x) for x in xrange(10))

    work perfectly well using the existing syntax.


    --
    Steven

    Comment

    • Lie Ryan

      #3
      Re: set/dict comp in Py2.6

      On Sat, 25 Oct 2008 09:07:35 +0000, Steven D'Aprano wrote:
      On Sat, 25 Oct 2008 01:13:08 -0700, bearophileHUGS wrote:
      >
      >I'd like to know why Python 2.6 doesn't have the syntax to create sets/
      >dicts of Python 3.0, like:
      >>
      >{x*x for x in xrange(10)}
      >{x:x*x for x in xrange(10)}
      >
      Maybe nobody asked for it?
      >
      Personally, I don't see the advantage of set and dict comprehensions.
      In fact, it is a good syntax sugar for set/dict(generator-comprehension)
      I
      think the value of them is very marginal, not worth the additional
      syntax.
      >
      set([x*x for x in xrange(10)])
      <nitpick>
      You should omit the []s as it would force python to build an internal
      list. I'm sure you know this would be a problem for large comprehensions.
      </nitpick>
      dict((x, x*x) for x in xrange(10))
      >
      work perfectly well using the existing syntax.
      >
      >
      --
      Steven

      Comment

      • Paul Rubin

        #4
        Re: set/dict comp in Py2.6

        bearophileHUGS@ lycos.com writes:
        {x*x for x in xrange(10)}
        {x:x*x for x in xrange(10)}
        I've always just used:

        set(x*x for x in xrange(10))
        dict((x,x*x) for x in xrange(10))

        I didn't even realize that you could write sets with {...}.

        Comment

        • bearophileHUGS@lycos.com

          #5
          Re: set/dict comp in Py2.6

          Sorry for the answering delay, Google Groups is slow today.

          Steven D'Aprano:
          >Personally, I don't see the advantage of set and dict comprehensions. I think the value of them is very marginal, not worth the additional syntax.<
          If it's worth in 3.0 then it's worth in 2.6 too. If it isn't worth in
          2.6 then maybe it's not worth in 3.0 too.

          It's just a little bit of sugar, and in this specific case I don't see
          risk of "diabetes".

          I think the dict generator syntax has a small advantage (the set
          generator is probably there just for symmetry): it redueces the number
          of perenthesys, and replaces a comma with a different symbol (a colon,
          this helps you to distinguish the colon itself from the other commas),
          this increases readability (Lisp docet).

          If the example is very simple like this you don't see much readability
          difference:
          sqrts = dict((x, x*x) for x in range(1000))
          sqrts = {x: x*x for x in range(1000)}

          But if those x and x*x need perenthesys then you may see a difference:
          sqrts = dict( ((sin(x) + 5) * 3, (x, (x*x, x*x*x))) for x in
          range(1000) )
          sqrts = {(sin(x) + 5) * 3: (x, (x*x, x*x*x)) for x in range(1000)}

          What's the more readable? I think the in second one is much simpler to
          tell if it's a correct expression, even after I have added extra
          spaces in the first line.


          And have you even received this error?
          >>dict(x,x*x for x in xrange(10))
          File "<stdin>", line 1
          SyntaxError: Generator expression must be parenthesized if not sole
          argument

          This syntax avoid that class of errors:
          {x:x*x for x in xrange(10)}

          So summing up I like the new syntax (I think Fortress language has
          something similar).

          Bye,
          bearophile

          Comment

          • Benjamin

            #6
            Re: set/dict comp in Py2.6

            On Oct 25, 3:13 am, bearophileH...@ lycos.com wrote:
            I'd like to know why Python 2.6 doesn't have the syntax to create sets/
            dicts of Python 3.0, like:
            Because nobody bothered to backport them.
            >
            {x*x for x in xrange(10)}
            {x:x*x for x in xrange(10)}
            >
            Bye,
            bearophile

            Comment

            • Benjamin

              #7
              Re: set/dict comp in Py2.6

              On Oct 25, 3:13 am, bearophileH...@ lycos.com wrote:
              I'd like to know why Python 2.6 doesn't have the syntax to create sets/
              dicts of Python 3.0, like:
              Because nobody bothered to backport them.
              >
              {x*x for x in xrange(10)}
              {x:x*x for x in xrange(10)}
              >
              Bye,
              bearophile

              Comment

              • Benjamin

                #8
                Re: set/dict comp in Py2.6

                On Oct 25, 3:13 am, bearophileH...@ lycos.com wrote:
                I'd like to know why Python 2.6 doesn't have the syntax to create sets/
                dicts of Python 3.0, like:
                Because nobody bothered to backport it.
                >
                {x*x for x in xrange(10)}
                {x:x*x for x in xrange(10)}
                >
                Bye,
                bearophile

                Comment

                • Benjamin

                  #9
                  Re: set/dict comp in Py2.6

                  On Oct 25, 3:13 am, bearophileH...@ lycos.com wrote:
                  I'd like to know why Python 2.6 doesn't have the syntax to create sets/
                  dicts of Python 3.0, like:
                  Because nobody bothered to backport it.
                  >
                  {x*x for x in xrange(10)}
                  {x:x*x for x in xrange(10)}
                  >
                  Bye,
                  bearophile

                  Comment

                  • Gabriel Genellina

                    #10
                    Re: set/dict comp in Py2.6

                    En Sat, 25 Oct 2008 23:44:46 -0200, Benjamin <musiccompositi on@gmail.com>
                    escribió:
                    On Oct 25, 3:13 am, bearophileH...@ lycos.com wrote:
                    >I'd like to know why Python 2.6 doesn't have the syntax to create sets/
                    >dicts of Python 3.0, like:
                    >
                    Because nobody bothered to backport them.
                    En Sat, 25 Oct 2008 23:47:32 -0200, Benjamin <musiccompositi on@gmail.com>
                    escribió:
                    Because nobody bothered to backport them.
                    En Mon, 27 Oct 2008 00:17:20 -0200, Benjamin <musiccompositi on@gmail.com>
                    escribió:
                    Because nobody bothered to backport it.
                    En Mon, 27 Oct 2008 00:19:29 -0200, Benjamin
                    <musiccompositi on@gmail.comesc ribió:
                    Because nobody bothered to backport it.
                    Yeah, we already know... :)

                    --
                    Gabriel Genellina

                    Comment

                    • Benjamin

                      #11
                      Re: set/dict comp in Py2.6

                      On Oct 27, 3:38 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
                      wrote:
                      En Sat, 25 Oct 2008 23:44:46 -0200, Benjamin <musiccomposit. ..@gmail.com> 
                      escribió:
                      >
                      On Oct 25, 3:13 am, bearophileH...@ lycos.com wrote:
                      I'd like to know why Python 2.6 doesn't have the syntax to create sets/
                      dicts of Python 3.0, like:
                      >
                      Because nobody bothered to backport them.
                      >
                      En Sat, 25 Oct 2008 23:47:32 -0200, Benjamin <musiccomposit. ..@gmail.com> 
                      escribió:
                      >
                      Because nobody bothered to backport them.
                      >
                      En Mon, 27 Oct 2008 00:17:20 -0200, Benjamin <musiccomposit. ..@gmail.com> 
                      escribió:
                      >
                      Because nobody bothered to backport it.
                      En Mon, 27 Oct 2008 00:19:29 -0200, Benjamin  
                      <musiccomposit. ..@gmail.comesc ribió:
                      Because nobody bothered to backport it.
                      >
                      Yeah, we already know... :)
                      Sorry about that.
                      >
                      --
                      Gabriel Genellina

                      Comment

                      Working...