[ python-Bugs-877675 ] Dictionary comprehension

SourceForge.net noreply at sourceforge.net
Thu Jan 15 17:29:51 EST 2004


Bugs item #877675, was opened at 2004-01-15 11:57
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=877675&group_id=5470

Category: Python Interpreter Core
Group: Feature Request
>Status: Closed
>Resolution: Later
Priority: 5
Submitted By: James Bond (james_007_bond)
Assigned to: Nobody/Anonymous (nobody)
Summary: Dictionary comprehension

Initial Comment:
Would it be possible to extend the list comprehension to 
dictionaries ? 

I found myself typing a line of the form:

dict = { x: x**2 for x in range(10) }

which seems pretty clear to me... But the interpreter is 
telling me it's not supported! Damn! I had to go through 
creating a list instead, and then use it to create my 
dictionary. 

dict = dict([(x, x**2) for x in range(10)])

By extending the dictionary syntax it seems the intermediate 
list creation could be avoided, and my proposed code looks 
clearer to me.

[using Python 2.3.3, on MacOS X 10.3.2]


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2004-01-15 17:29

Message:
Logged In: YES 
user_id=80475

This feature is subsumed by generator expressions which have
been  accepted for Py2.4.  It will basically give you what
your look for (list comp style expression without creating
the intermediate list):

d = dict((x, x**2) for x in xrange(10))



----------------------------------------------------------------------

Comment By: Barry A. Warsaw (bwarsaw)
Date: 2004-01-15 12:29

Message:
Logged In: YES 
user_id=12800

See PEP 274

http://www.python.org/peps/pep-0274.html

I like the idea too, but it doesn't seem to have gained
widespread support yet.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=877675&group_id=5470



More information about the Python-bugs-list mailing list