set/dict comp in Py2.6

Lie Ryan lie.1296 at gmail.com
Sat Oct 25 11:24:39 EDT 2008


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





More information about the Python-list mailing list