Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Python 2.6 have set literals and comprehensions or dict comprehensions? [closed]

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?

like image 589
Joe Shaw Avatar asked Dec 05 '08 22:12

Joe Shaw


People also ask

What is comprehensions in Python?

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.

Are there set literals in Python 2?

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):

What is the basic structure of dictionary comprehension in Python?

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.

How to create a dictionary in Python?

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.


2 Answers

It wasn't done because nobody took the time to do it. There are bugs opened for months, and no one commented on them:

  • http://bugs.python.org/issue2333
  • http://bugs.python.org/issue2334
  • http://bugs.python.org/issue2335

So it wasn't important enough for anybody to care, probably.

like image 183
Thomas Hervé Avatar answered Oct 23 '22 12:10

Thomas Hervé


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?

like image 42
tzot Avatar answered Oct 23 '22 12:10

tzot