There are three different ways of converting two lists into key:value pair dictionary.
Lets look at them one by one (using above given example):
1) Naive method:
By simply declaring a dictionary and then running a nested loop for both lists and assigning key:value pairs.
#initializing lists
keys = ['Name','Emp ID','Contact Info']
values = ['Ken','ED445','#########']
#to convert list into dictionary
res = {}
for key in keys:
for value in values:
res[key] = value
values.remove(value)
break
#print dictionary
print ("dict : " + str(res))
Output:
dict : {'Name': 'Ken', 'Emp ID': 'ED445', 'Contact Info': '#########'}
2) Using zip() method
Here one list elements are paired with the another list elements at corresponding index in key:value pair.
# initializing lists
keys = ['Name','Emp ID','Contact Info']
values = ['Ken','ED445','#########']
#using zip() convert list into dictionary
res = dict(zip(keys, values))
#print dictionary
print ("dict : " + str(res))
Output:
dict : {'Name': 'Ken', 'Emp ID': 'ED445', 'Contact Info': '#########'}
3) Using dictionary comprehension
# initializing lists
keys = ['Name','Emp ID','Contact Info']
values = ['Ken','ED445','#########']
#using dictionary comprehension to convert list into dictionary
res = {keys[i]: values[i] for i in range(len(keys))}
#print dictionary
print ("dict : " + str(res))
Output:
dict : {'Name': 'Ken', 'Emp ID': 'ED445', 'Contact Info': '#########'}
2nd and 3rd method are faster and time saving as it involves less code.