Faster (smarter?) dictionary building

Michael T. Babcock mbabcock at fibrespeed.net
Thu Oct 30 14:45:48 EST 2003


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 
falling back on the more C-like:

for i in range(len(headings)):
    d[h[i]] = v[i]

Is there anything somewhat cleaner or more pythonesque I could do 
instead?  Thanks.

-- 
Michael T. Babcock
C.T.O., FibreSpeed Ltd.
http://www.fibrespeed.net/~mbabcock







More information about the Python-list mailing list