Python 2.6 was basically a stepping stone to make converting to Python 3 easier. A lot of the features destined for Python 3 were implemented in 2.6 if they didn't break backward compatibility with syntax and the class libs.
Why weren't set literals ({1, 2, 3}
), set comprehensions ({v for v in l}
), or dict comprehensions ({k: v for k, v in d}
) among them? In particular dict comprehensions would have been a great boon... I find myself using the considerably uglier dict([(k, v) for k, v in d])
an awful lot lately.
Is there something obvious I'm missing, or was this just a feature that didn't make the cut?
Comprehensions in Python provide us with a short and concise way to construct new sequences (such as lists, set, dictionary etc.) using sequences which have been already defined. Python supports the following 4 types of comprehensions: List Comprehensions. Dictionary Comprehensions.
There were no set literals in Python 2, historically curly braces were only used for dictionaries. Sets could be produced from lists (or any iterables):
The basic structure of a dictionary comprehension looks like below. output_dict = {key:value for (key, value) in iterable if (key, value satisfy this condition)} Example #1: Suppose we want to create an output dictionary which contains only the odd numbers that are present in the input list as keys and their cubes as values.
Like List Comprehension, Python allows dictionary comprehensions. We can create dictionaries using simple expressions. Let’s see a example,lets assume we have two lists named keys and value now, We can use Dictionary comprehensions with if and else statements and with other expressions too.
It wasn't done because nobody took the time to do it. There are bugs opened for months, and no one commented on them:
So it wasn't important enough for anybody to care, probably.
All these are syntax/grammar changes. Such changes are traditionally introduced first in a Python x.y version with a from __future__ import …
statement, and implemented at least on Python x.(y+1) version. Such a transition hasn't happened yet for these changes.
Technically, I've answered your "why".
Now, if you meant, “why didn't anyone take the time to suggest, support and implement something that I would like to have in 2.x also, even if they don't know about it since I never tried to suggest/support backporting those syntax enhancements in either comp.lang.python or Python-Dev and I never tried to even read the PEPs?”, then the answer lies in you too, and you can offer an answer yourself.
HTH
BTW, you shouldn't use the dict([(k,v) for k,v in d])
form, but the dict((k,v) for k,v in d)
. More efficient. Why create an intermediate list?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With