Convert Two Lists Into a Dictionary in Python Using Dictionary Comprehension

In this blog post, we will explore how to convert two lists into a dictionary in Python using dictionary comprehension. By the end of this blog, you will grasp the concept of combining lists into a dictionary and gain insights into the practical applications of this technique.

Python provides a huge range of built-in functions that make programming tasks efficient and easy. One such dictionary comprehension method that allows us to combine two lists into a dictionary effortlessly. This powerful feature simplifies the transformation of data structures, enabling us to handle key-value pairs more effectively.

So, let’s dive into the world of Python and write a Python program to convert/combine two lists into a dictionary using dictionary comprehension.

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))

Explanation for the Code:

Initialising the lists

We start by initialising two lists: test_keys and test_values. The test_keys list contains keys for the dictionary, and the test_values list contains corresponding values. 

Printing the original lists

We print the original lists to see their contents before converting them into a dictionary. This step helps us verify the original data.

Using dictionary comprehension to convert lists to a dictionary

We use dictionary comprehension, a concise way to create dictionaries, to convert the lists into a dictionary. The syntax for dictionary comprehension is {key_expression: value_expression for item in iterable}. In our code, we iterate over the indices of test_keys using range(len(test_keys)). For each index i, we assign test_keys[i] as the key and test_values[i] as the value in the resulting dictionary.

Printing the resultant dictionary

We print the resultant dictionary to see the conversion outcome. This dictionary now contains the elements from the original lists, with the names as keys and corresponding scores as values.

Output:

Original key list is : ['Chandan', 'Pratik', 'Praful']
Original value list is : [3, 5, 7]
Resultant dictionary is : {'Chandan': 3, 'Pratik': 5, 'Praful': 7}

The program takes two lists: test_keys and test_values. The test_keys list contains names, and the test_values list contains corresponding numbers. The program then combines these two lists into a dictionary using the zip() function. The resulting dictionary pairs each name with its corresponding number.

Here are a few alternatives for this program:

Using a for loop:

You can iterate over the indices of one list and use them to access the corresponding elements from the other list. Create an empty dictionary and populate it by assigning each key-value pair. This method requires more manual handling of the indices and accessing elements individually.

Using enumerate() function:

It provides a concise and readable solution. It explicitly associates the values with their indices in a compact form, avoiding the need for additional lambda functions or explicit loops.

Using the map() function:

You can simplify the code by applying the lambda function to each corresponding element of the two lists, creating a map object. By converting the map object into a list of tuples and then into a dictionary, you achieve the desired result of converting the two lists into a dictionary.

Using dict() with zip() function:

Instead of using a list comprehension with enumerate(), you can directly pass the two lists to zip(). zip() combines the elements of both lists into pairs, and dict() converts the resulting list of pairs into a dictionary. This method simplifies the code by eliminating the need for enumerate() and the list comprehension.

Conclusion

In conclusion, the code shows how to convert or combine two lists into a dictionary using dictionary comprehension in Python. By using dictionary comprehension, we can pair the corresponding elements from the two lists and create key-value pairs in the resultant dictionary. This provides an easy way to organize data and access values based on specific keys.

About The Author

Leave a Reply