Is there something similar to list comprehension in dict?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Fri Nov 20 15:37:22 EST 2009
On Fri, 20 Nov 2009 03:08:01 -0800, DreiJane wrote:
> NB: I wondered about about dict(one=1, two=2) - why not d = {one:1,
> two:2} ?
Because it doesn't work unless you have defined names one and two.
dict(one=1, two=2) uses keyword arguments, namely one and two. This is
the same standard mechanism by which you call functions with keyword
arguments:
myfunc(widget=x, width=5, name='fred', flag=True)
The dict literal syntax requires names one and two to already exist,
otherwise you have to quote them to make them strings:
d = {'one': 1, 'two': 2}
> Since you do not write L=list((1, 2)) either.
But you certainly can. It would be wasteful, since first it constructs a
tuple (1, 2), then it creates a list from that tuple.
> These composed
> objects as basic building blocks make Python code so dense and
> beautiful, thus using "{}" means embracing the language's concept.
I don't understand this sentence.
--
Steven
More information about the Python-list
mailing list