Convert Two Lists Into a Dictionary in Python Using for Loop
(Last Updated On: 20/03/2023)
Python Program to Convert / Combine Two Lists Into a Dictionary Using for Loop
# Convert or Combine Two Lists Into a Dictionary in python using Naive Method/for loop
# Python3 code to demonstrate
# conversion of lists to dictionary
# using naive method
# initializing lists
test_keys = ["Chandan", "Pratik", "Praful"]
test_values = [3, 5, 7]
# Printing original keys-value lists
print("Original key list is : " + str(test_keys))
print("Original value list is : " + str(test_values))
# using naive method
# to convert lists to dictionary
res = {}
for key in test_keys:
for value in test_values:
res[key] = value
test_values.remove(value)
break
# Printing resultant dictionary
print("Resultant dictionary is : " + str(res))
Output:
Original key list is : ['Chandan', 'Pratik', 'Praful']
Original value list is : [3, 5, 7]
Resultant dictionary is : {'Chandan': 3, 'Pratik': 5, 'Praful': 7}