You can use the zip() to combine two lists into a dictionary in Python. We can use the dict() constructor and dictionary comprehension along with the zip() to combine into the dictionary very easily. In this article, I will explain how we can zip the two lists into the dictionary in Python by using zip() function.
1. Quick Examples of Zip Lists into Dictionary
Following are quick examples of zipping the lists into a dictionary.
# Quick examples of zip the Python dictionary
subject_id = [1,2,3,4,5]
subject_name = [“PHP”,”Java”,”Python”,”R”,”Jsp”]
# Example 1: Zip the dictionary Using zip() & dict()
zip_dict = dict(zip(subject_id, subject_name))
# Example 2: Using zip() and dictionary comprehension
# to zip the dictionary
zip_dict = {subject_id: subject_name for subject_id, subject_name in zip(subject_id, subject_name)}
# Example 3: Zip the dictionary by passing dictionaries
# into zip()
zip_dict = dict(zip(subjects1.keys(), subjects2.values()))
2. Zip Lists into Dictionary in Python
The zip() is a built-in function of Python that is used to combine passed arguments and return them as a series of tuples and then typecast it to the dictionary, when we apply type casting we can get the desired output.
When we wanted to convert two iterable objects into a Python dictionary, using zip() will consider the elements of the first iterable as keys and the elements of the second iterable as values.
Let’s create two lists and pass them into the zip() function, it will return the iterable of tuples. After that, we will use the dict() constructor to convert the zipped result into a dictionary.
# Initialize the list
subject_id = [1,2,3,4,5]
subject_name = [“PHP”,”Java”,”Python”,”R”,”Jsp”]
print(“List1:”, subject_id)
print(“List2:”, subject_name)
# Zip the dictionary Using zip() & dict()
zip_dict = dict(zip(subject_id, subject_name))
print(“Zipped dictioanry:”, zip_dict)
Yields below output.
As we can see from the above the dictionary has been created from two lists.
3. Zip the Dictionary Using zip() & Dictionary Comprehension
Alternatively, we can use the zip() function along with dictionary comprehension. Using dict comprehension we can create dictionaries very easily with concise code. Let’s create two lists, subject_id_list with 5 integers and subject_name_list with 5 strings. Now, zip these two by having integers as keys and strings as values.
# Using zip() and dictionary comprehension
# to zip the dictionary
print(“List1:”, subject_id)
print(“List2:”, subject_name)
# Zip into dictionary
zip_dict = {subject_id: subject_name for subject_id, subject_name in zip(subject_id, subject_name)}
print(“Zipped dictionary:”, zip_dict)
Yields below output.
4. Zip two Dictionaries in Python
So far, we have learned how to zip two lists into a dictionary using zip() function. Now we will learn how to zip two dictionaries by passing two dictionaries into zip() function. Let’s see the below example,
# Create dictionaries
subjects1={1: ‘PHP’, 2: ‘Python’}
subjects2={3: ‘Java’, 4: ‘CSS’}
print(“Dictionary1:”, subjects1)
print(“Dictionary2:”, subjects2)
# Zip the dictionaries
zip_dict = dict(zip(subjects1.keys(), subjects2.values()))
print(“Zipped dictionary:”, zip_dict)
# Output:
# Dictionary1: {1: ‘PHP’, 2: ‘Python’}
# Dictionary2: {3: ‘Java’, 4: ‘CSS’}
# Zipped dictionary: {1: ‘Java’, 2: ‘CSS’}
As you can see the dictionary has been created by taking keys from the keys of the first dictionary and values from the values of the second dictionary.
5. Update the Dictionary using zip()
# Create dictionaries
subjects1={1: ‘PHP’, 2: ‘Python’}
subjects2={3: ‘Java’, 4: ‘CSS’}
print(“Dictionary1:”, subjects1)
print(“Dictionary2:”, subjects2)
# Zip the dictionary by passing dictionaries
# into zip()
zip_dict = dict(zip(subjects1.keys(), subjects2.values()))
print(“Zipped dictionary:”, zip_dict)
# Output:
# Dictionary1: {1: ‘PHP’, 2: ‘Python’}
# Dictionary2: {3: ‘Java’, 4: ‘CSS’}
# Zipped dictionary: {1: ‘Java’, 2: ‘CSS’}
6. Conclusion
In this article, I have explained the Python zip() function and using this syntax and parameters how we can zip the lists into a dictionary. I have explained using dictionary comprehension and the dict() constructor along with zip() function.
You can use the zip() to combine two lists into a dictionary in Python. We can use the dict() constructor and dictionary comprehension along with the zip() to combine into the dictionary very easily. In this article, I will explain how we can zip the two lists into the dictionary in Python by using zip() Read More Python, Python Tutorial, Python Dictionary Examples