Connecting Tech Pros Worldwide Forums | Help | Site Map

set/dict comp in Py2.6

bearophileHUGS@lycos.com
Guest
 
Posts: n/a
#1: Oct 25 '08
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
Guest
 
Posts: n/a
#2: Oct 25 '08

re: set/dict comp in Py2.6


On Sat, 25 Oct 2008 01:13:08 -0700, bearophileHUGS wrote:
Quote:
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

Lie Ryan
Guest
 
Posts: n/a
#3: Oct 25 '08

re: set/dict comp in Py2.6


On Sat, 25 Oct 2008 09:07:35 +0000, Steven D'Aprano wrote:
Quote:
On Sat, 25 Oct 2008 01:13:08 -0700, bearophileHUGS wrote:
>
Quote:
>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)
Quote:
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>
Quote:
dict((x, x*x) for x in xrange(10))
>
work perfectly well using the existing syntax.
>
>
--
Steven

Paul Rubin
Guest
 
Posts: n/a
#4: Oct 25 '08

re: set/dict comp in Py2.6


bearophileHUGS@lycos.com writes:
Quote:
{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 {...}.
bearophileHUGS@lycos.com
Guest
 
Posts: n/a
#5: Oct 25 '08

re: set/dict comp in Py2.6


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

Steven D'Aprano:
Quote:
>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?
Quote:
Quote:
Quote:
>>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
Benjamin
Guest
 
Posts: n/a
#6: Oct 26 '08

re: set/dict comp in Py2.6


On Oct 25, 3:13*am, bearophileH...@lycos.com wrote:
Quote:
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.
Quote:
>
{x*x for x in xrange(10)}
{x:x*x for x in xrange(10)}
>
Bye,
bearophile
Benjamin
Guest
 
Posts: n/a
#7: Oct 26 '08

re: set/dict comp in Py2.6


On Oct 25, 3:13*am, bearophileH...@lycos.com wrote:
Quote:
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.
Quote:
>
{x*x for x in xrange(10)}
{x:x*x for x in xrange(10)}
>
Bye,
bearophile
Benjamin
Guest
 
Posts: n/a
#8: Oct 27 '08

re: set/dict comp in Py2.6


On Oct 25, 3:13*am, bearophileH...@lycos.com wrote:
Quote:
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.
Quote:
>
{x*x for x in xrange(10)}
{x:x*x for x in xrange(10)}
>
Bye,
bearophile
Benjamin
Guest
 
Posts: n/a
#9: Oct 27 '08

re: set/dict comp in Py2.6


On Oct 25, 3:13*am, bearophileH...@lycos.com wrote:
Quote:
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.
Quote:
>
{x*x for x in xrange(10)}
{x:x*x for x in xrange(10)}
>
Bye,
bearophile
Gabriel Genellina
Guest
 
Posts: n/a
#10: Oct 27 '08

re: set/dict comp in Py2.6


En Sat, 25 Oct 2008 23:44:46 -0200, Benjamin <musiccomposition@gmail.com>
escribió:
Quote:
On Oct 25, 3:13*am, bearophileH...@lycos.com wrote:
Quote:
>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 <musiccomposition@gmail.com>
escribió:
Quote:
Because nobody bothered to backport them.
En Mon, 27 Oct 2008 00:17:20 -0200, Benjamin <musiccomposition@gmail.com>
escribió:
Quote:
Because nobody bothered to backport it.
Quote:
En Mon, 27 Oct 2008 00:19:29 -0200, Benjamin
<musiccomposition@gmail.comescribió:
Quote:
Because nobody bothered to backport it.
Yeah, we already know... :)

--
Gabriel Genellina

Benjamin
Guest
 
Posts: n/a
#11: Oct 30 '08

re: set/dict comp in Py2.6


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