Is there something similar to list comprehension in dict?

Diez B. Roggisch deets at nospam.web.de
Fri Nov 20 09:03:35 EST 2009


DreiJane schrieb:
> NB: I wondered about about dict(one=1, two=2) - why not d = {one:1,
> two:2} ? Since you do not write L=list((1, 2)) either. These composed

because it's not working.

 >>> {one : 1}
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
NameError: name 'one' is not defined


Yes, that looks nitpicky, but that is exactly the reason one often 
prefers the dict(...)-variant. Because it uses python keywords, it 
spares you to type quotes around all the keys. Which IMHO is more aesthetic.


> objects as basic building blocks make Python code so dense and
> beautiful, thus using "{}" means embracing the language's concept.

The collection-literals are a great thing, no doubt. But these 
alternatives are not against any concept.

Diez



More information about the Python-list mailing list