Convert Two Lists Into a Dictionary in Python Using map() Function

(Last Updated On: 11/10/2023)

In this blog post, we will explore a simple and elegant method to convert two lists into a dictionary in Python using map() function. We will dive into the step-by-step logic of the code and explain each operation in a straightforward manner. Additionally, we will discuss alternative approaches and the advantages of using the map() function for this particular task.

Have you ever come across a situation where you have two lists of data that you want to combine into a dictionary in Python? Perhaps you have a list of names and a corresponding list of values, and you need to pair them up for easy lookup and manipulation. This is a common scenario in programming, and luckily, Python offers several ways to accomplish this task efficiently.

So, let’s get started and compile a Python program to convert/combine two lists Into a dictionary using the map() function.

Python Program to Convert / Combine Two Lists Into a Dictionary Using map() Function

# Convert or Combine Two Lists Into a Dictionary in python Using map()

# Python3 code to demonstrate
# conversion of lists to dictionary
# using dict() + map()

# initializing lists
keys = ["Chandan", "Pratik", "Praful"]
values = [3, 5, 7]

# Printing original keys-value lists
print ("Original key list is : " + str(keys))
print ("Original value list is : " + str(values))

# using map and dict type casting
# to convert lists to dictionary
res = dict(map(lambda i,j : (i,j) , keys,values))

# Printing resultant dictionary
print ("Resultant dictionary is : " + str(res))

Step-by-step Explanation of Code:

Initialise the lists:

In this step, we initialise two lists: keys and values. The keys list contains the keys that will be used in the dictionary. The values list contains the corresponding values for each key.

Print the original key-value lists:

In this step, we print the original keys and values lists to show their contents before converting them into a dictionary.

Convert lists to a dictionary using map() and dict():

In this step, we use the map() function along with lambda and dict() to convert the two lists into a dictionary. The lambda function takes two arguments, i and j, which represent the corresponding elements from the keys and values lists.

The map() function creates a sequence of key-value pairs and the dict() function then converts this sequence of pairs into a dictionary.

Print the resultant dictionary:

In this step, we print the resultant dictionary after converting the lists. The res variable holds the dictionary obtained from the conversion. We use the str() function to convert the dictionary to a string representation before printing it.

Output:

When the above code is executed, the output will be as follows:

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

The output displays the original keys and values lists, as well as the resultant dictionary obtained from their conversion.

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

Using a dictionary comprehension:

You can use dictionary comprehension to iterate over both lists simultaneously and construct the dictionary in a more concise way. The dictionary comprehension creates key-value pairs directly from the two lists without needing zip() or dict().

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.

Conclusion:

This code shows a simple method to convert two lists into a dictionary using the map() function, lambda expressions, and the dict() function in Python. This approach offers a short and readable solution, allowing you to map corresponding elements from the key list to the value list efficiently. By utilising the map() function, the code avoids the need for explicit loops.

This method provides a practical and efficient way to create dictionaries from lists, particularly when dealing with large datasets. With its clean syntax, this approach is a valuable tool in your Python programming skillset for converting lists into dictionaries.

Frequently Asked Questions

Q: What is a lambda expression?

A: A lambda expression is an anonymous function that can take multiple commands and perform a single output. It is commonly used with functions like map().

Q: Why is the dict() function used in this code?

A: The dict() function converts the iterable produced by map() into a dictionary. It takes a sequence of key-value pairs and returns a dictionary object.

Q: What happens if the key and value lists have different lengths?

A: If the key and value lists have different lengths, the resulting dictionary will only contain pairs up to the length of the shorter list.

Q: What are the advantages of using map() over other methods?

A: The map() approach reduces the need for explicit loops, resulting in cleaner and more readable code. It also emphasises the functional programming aspect of Python.

About The Author

Leave a Reply