Skip to content Skip to sidebar Skip to footer

How To Add Two Lists Into Dictionary?

i=['Pin','Type','value'] j=[['abc','input','1234'],['xyz','output','2345'],['pqr','input','567']] z=dict(zip(i,j)) And I want to join them into dictionary, so that my output shoul

Solution 1:

>>> dict(zip(i,zip(*j)))
{'Type': ('input', 'output', 'input'), 'value': ('1234', '2345', '567'), 'Pin': ('abc', 'xyz', 'pqr')}

Or if you really want lists,

>>> dict(zip(i,map(list,zip(*j))))
{'Type': ['input', 'output', 'input'], 'value': ['1234', '2345', '567'], 'Pin': ['abc', 'xyz', 'pqr']}

izip, imap, etc may be appropriate if the lists were longer.

Post a Comment for "How To Add Two Lists Into Dictionary?"