a dictionary from a list

Roy Smith roy at panix.com
Sat Jun 25 15:10:33 CEST 2005

Terry Hancock <hancock at anansispaceworks.com> wrote:
> Before the dict constructor, you needed to do this:
> d={}
> for key in alist:
>     d[key]=None

I just re-read the documentation on the dict() constructor.  Why does it 
support keyword arguments?

   dict(foo="bar", baz="blah") ==> {"foo":"bar", "baz"="blah"}

This smacks of creeping featurism.  Is this actually useful in real code?  
It took me several readings of the doc to understand what this was doing.  
Essentially, it's Perl's bareword syntax, and once I realized that, I 
simultaneously understood what was happening and was revolted that Python 
seems to have picked up one of Perl's most bizarre and confusing features.

I also think the published description is needlessly confusing.  Why does 
it use

   {'one': 2, 'two': 3}

as the example mapping when

   {'one': 1, 'two': 2}

would illustrate exactly the same point but be easier to comprehend.  The 
mapping given is the kind of thing I would expect to see in an obfuscated 
programming contest.

Also, what's the point of the last example:

   dict([(['one', 'two'][i-2], i) for i in (2, 3)])

It boils down to passing a list of tuples as an argument, which is already 
illustrated by other examples.  This is just a complicated and obtuse way 
to construct the list of tuples.  What does it add to the understanding of 
how the dict() constructor works?

More information about the Python-list mailing list