Convert Two Lists Into a Dictionary in Python Using Dictionary Comprehension

(Last Updated On: 20/03/2023)

Python Program to Convert / Combine Two Lists Into a Dictionary Using Dictionary Comprehension

# Convert Two Lists Into a Dictionary Using dictionary comprehension in python

# Python3 code to demonstrate
# conversion of lists to dictionary
# using dictionary comprehension

# 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 dictionary comprehension
# to convert lists to dictionary
res = {test_keys[i]: test_values[i] for i in range(len(test_keys))}

# 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}

Leave a Reply

Your email address will not be published. Required fields are marked *