Python two list to dictionary method

Python two list to dictionary method

Original List
kList = ['a', 'b', 'c']
vList = ['1', 2, '3']

Convert to the following dictionary
dstDict = {'a' : '1', '2' : 42, 'c' : '3'}


Method 1:
dstDict = dict(zip(kList, vList))

Method 2:
dstDict = {k:v for k, v in zip(kList, vList)}

Method 3:
dstDict = {kList[i] : vList[i] for i in range(len(kList))}

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example