How to convert two lists into a dictionary ?

May 7, 2021, 4:07 p.m.


...

Create an empty dictionary, then using two for loops You can insert data into the dictionary consider first list as a key and another as its value. In this way, you can create a dictionary using two lists or you can use zip function to do it.

Create an empty dictionary, then using two for loops You can insert data into the dictionary consider first list as a key and another as its value. In this way, you can create a dictionary using two lists or you can use zip function to do it.

test_keys=['Aman', 'Pankaj', 'Atul']
test_values=[1,4,5]
res={}
for key in test_keys:
 for value in test_values:
  res[key]=value
  test_values.remove(value)
  break
print(res)
test_keys=['Aman', 'Pankaj', 'Atul']
test_values=[1,4,5]
res=dict(zip(test_keys,test_values))
print(res)



Tags


Comments