Faster (smarter?) dictionary building

Jay Dorsey jay at jaydorsey.com
Thu Oct 30 14:46:25 EST 2003


Michael T. Babcock 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.
> 

How about:

 >>> headings, values = ['a', 'b', 'c'], [1, 2, 3]
 >>> d = {}
 >>> for h, v in zip(headings, values):
...     d[h] = v
...
 >>> d
{'a':1, 'c':3, 'b':2}

Jay






More information about the Python-list mailing list