473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Oct 25 '08 #1
10 1691
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
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

Oct 25 '08 #3
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
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
Oct 25 '08 #5
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
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
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
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
En Sat, 25 Oct 2008 23:44:46 -0200, Benjamin <mu**************@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 <mu**************@gmail.com>
escribió:
Because nobody bothered to backport them.
En Mon, 27 Oct 2008 00:17:20 -0200, Benjamin <mu**************@gmail.com>
escribió:
Because nobody bothered to backport it.
En Mon, 27 Oct 2008 00:19:29 -0200, Benjamin
<mu**************@gmail.comescribió:
Because nobody bothered to backport it.
Yeah, we already know... :)

--
Gabriel Genellina

Oct 27 '08 #10
On Oct 27, 3:38*am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
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.comescribió:
Because nobody bothered to backport it.

Yeah, we already know... :)
Sorry about that.
>
--
Gabriel Genellina
Oct 30 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Xavier | last post by:
Greetings, (do excuse the possibly comical subject text) I need advice on how I can convert a text db into a dict. Here is an example of what I need done. some example data lines in the...
9
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a variable length list of associated values after...
2
by: GrelEns | last post by:
hello, i would like if this behaviour can be obtained from python : trap an attributeError from inside a subclassing dict class... (here is a silly examples to explain my question) class...
7
by: Marcio Rosa da Silva | last post by:
Hi! In dictionaries, unlinke lists, it doesn't matter the order one inserts the contents, elements are stored using its own rules. Ex: >>> d = {3: 4, 1: 2} >>> d {1: 2, 3: 4}
3
by: Bengt Richter | last post by:
Has anyone found a way besides not deriving from dict? Shouldn't there be a way? TIA (need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-) I guess I can just document...
11
by: sandravandale | last post by:
I can think of several messy ways of making a dict that sets a flag if it's been altered, but I have a hunch that experienced python programmers would probably have an easier (well maybe more...
15
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword arguments (e.g a default value or an orderby...
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
8
by: james_027 | last post by:
hi for example I have this dictionary dict = {'name':'james', 'language':'english'} value = 'sex' in dict and dict or 'unknown' is a right pythonic of doing this one? I am trying to get a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.