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

(Last Updated On: 11/10/2023)

In Python, there are times when we have two separate lists and want to combine them into a single data structure for easier handling and access. One common scenario is when we have a list of keys and a list of corresponding values, and we wish to create a dictionary from them.

The zip() function allows us to pair up elements from multiple iterables, such as lists, into tuples. By leveraging this powerful function, we can easily merge our key and value lists into a dictionary without the need for complex loops or manual operations.

So, let’s dive in and explore a Python program to convert/combine two lists into a dictionary using the zip() function.

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

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

# Python3 code to demonstrate
# conversion of lists to dictionary
# using zip()

# 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 zip()
# to convert lists to dictionary
res = dict(zip(test_keys, test_values))

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

Explanation of Code:

Initialising the Lists

In this step, we initialize two lists: test_keys and test_values. These lists contain the keys and values that we want to combine into a dictionary.

Printing the Original Lists

Here, we print the original key and value lists using the print() function. This helps us verify the initial content of the lists.

Combining Lists into a Dictionary Using zip()

In this step, we use the zip() function to combine the two lists into a dictionary. The zip() function takes two or more lists and pairs the corresponding elements together. We pass the test_keys and test_values lists as arguments to zip(). The resulting pairs are then converted into a dictionary using the dict() function.

Printing the Resultant Dictionary

Finally, we print the resultant dictionary using the print() function. This allows us to see the combined key-value pairs in the dictionary.

Output:

When we run the above code, we get the following 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 output shows the original key-value lists and the resultant dictionary that was created using the zip() function.

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

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.

We used the zip() method in our code as it provides a short and easy way to combine two lists into a dictionary with just a single line of code. It removes the need for difficult logic. It is highly optimised and efficient. It performs the pairing of elements quickly, making it suitable for large datasets. The zip() method conveys the intent of combining lists into a dictionary in a clear and intuitive manner. It allows you to combine multiple lists or even other types of sequences into a dictionary.

Conclusion

In this tutorial, the code explains a straightforward method to convert or combine two lists into a dictionary using the zip() function in Python. By using the zip() function, we can pair the corresponding elements from the key and value lists and transform them into a dictionary using the dict() function.

This approach offers a short and efficient solution for creating dictionaries from lists. By understanding this code, you now have a valuable tool at your disposal for converting and organising data in Python.

Frequently Asked Questions

Q: How does zip() convert the lists into a dictionary?

A: Bypassing the test_keys and test_values lists as arguments to zip(), the function pairs the elements from both lists and creates tuples. These tuples are then converted into a dictionary using the dict() function.

Q: What if the input lists have different lengths?

A: If the input lists have different lengths, the resulting dictionary will only contain key-value pairs up to the length of the shorter list. The extra elements in the longer list will be ignored.

Q: Can I use more than two lists with zip()?

A: Yes, the zip() function can be used with more than two lists. You can pass multiple lists as arguments to zip() to combine their elements into tuples.

Q: What if there are duplicate keys in the test_keys list?

A: If there are duplicate keys in the test_keys list, the resulting dictionary will only contain the last occurrence of each key. Duplicate keys are not allowed in dictionaries, as they are meant to have unique keys.

About The Author

Leave a Reply