Faster (smarter?) dictionary building

Bengt Richter bokr at oz.net
Thu Oct 30 17:19:41 EST 2003


On Thu, 30 Oct 2003 14:45:48 -0500, "Michael T. Babcock" <mbabcock at fibrespeed.net> wrote:

>I have a list of column headings and an array of values:
>headings, values = ['a', 'b', 'c'], [1, 2, 3]
>
>I want to construct a dictionary such that d['a'] = 1 and so on.
>
>The first way I tried to do this was:
>
>d = {}
>for h,v in headings, values:
>    d[h] = v
>
>It turns out this doesn't work, but it was worth a try.  I ended up

IMO it would be interesting to make that work, but spelling it like a tuple unpacking
assignment with a 'for' in front of it, to make it step through the sequences on the right. E.g,

 d = {}
 for h,v = headings, values: # illegal now, proposed lazy parallel sequence unpacking
     d[h] = v

would work as "expected", i.e., as if

 for h,v in itertools.izip(headings,values):
     d[h] = v

Regards,
Bengt Richter




More information about the Python-list mailing list