You can feed the results of zip()
to the builtin dict()
:
>>> names = [u'when_air', u'chrono', u'age_marker', u'name']
>>> print(dict(zip(names, range(4))))
{'chrono': 1, 'name': 3, 'age_marker': 2, 'when_air': 0}
zip()
will return a list of tuples, where each tuple is the ith element from names
and range(4)
. dict()
knows how to create a dictionary from that.
Notice that if you give sequences of uneven lengths to zip()
, the results are truncated. Thus it might be smart to use range(len(names))
as the argument, to guarantee an equal length.
>>> print(dict(zip(names, range(len(names)))))
{'chrono': 1, 'name': 3, 'age_marker': 2, 'when_air': 0}