455,587 Members | 1,666 Online
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

# set/dict comp in Py2.6

 P: n/a 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 Oct 25 '08 #1
10 Replies

 P: n/a 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 Oct 25 '08 #2

 P: n/a 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)]) 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. dict((x, x*x) for x in xrange(10)) work perfectly well using the existing syntax. -- Steven Oct 25 '08 #3

 P: n/a be************@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 {...}. Oct 25 '08 #4

 P: n/a 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 "", 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 Oct 25 '08 #5

 P: n/a 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 Oct 26 '08 #6

 P: n/a 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 Oct 26 '08 #7

 P: n/a 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 Oct 27 '08 #8

 P: n/a 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 Oct 27 '08 #9

 P: n/a En Sat, 25 Oct 2008 23:44:46 -0200, Benjamin 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 escribió: Because nobody bothered to backport them. En Mon, 27 Oct 2008 00:17:20 -0200, Benjamin escribió: Because nobody bothered to backport it. En Mon, 27 Oct 2008 00:19:29 -0200, Benjamin

 P: n/a On Oct 27, 3:38*am, "Gabriel Genellina" wrote: En Sat, 25 Oct 2008 23:44:46 -0200, Benjamin * 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 * escribió: Because nobody bothered to backport them. En Mon, 27 Oct 2008 00:17:20 -0200, Benjamin * escribió: Because nobody bothered to backport it. En Mon, 27 Oct 2008 00:19:29 -0200, Benjamin * -- Gabriel Genellina Oct 30 '08 #11

### This discussion thread is closed

Replies have been disabled for this discussion.