You are currently viewing How to Zip Dictionary in Python

You can use the zip() to combine two dictionaries into a dictionary and 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.

Advertisements

In this article, I will explain how we can zip two dictionaries into a single dictionary and zip the two lists into the dictionary in Python by using zip() function.

1. Quick Examples of Zip Dictionary

Following are quick examples of zipping the two dictionaries into a dictionary and two lists into a dictionary.


# Quick examples of zip the Python dictionary

# Initialize dictionary
subjects1={1: 'PHP', 2: 'Python'}
subjects2={3: 'Java', 4: 'CSS'}

# Example 1: Zip the dictionaries
zip_dict = list(zip(subjects1, subjects2))

# Example 2: Zip the dictionaries
zip_dict = list(zip(subjects1.keys(), subjects2.values()))

# Initialize lists
subject_id = [1,2,3,4,5]
subject_name = ["PHP","Java","Python","R","Jsp"]

# Example 3:  Zip the dictionary Using zip() & dict()
zip_dict = dict(zip(subject_id, subject_name))

# Example 4: 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)}

2. Zip two Dictionaries in Python

The zip() is a built-in function of Python that can be used to zip two dictionaries and return them as a series of tuples and then typecast it to the dictionary or list based on your need, when we apply type casting we can get the desired output.

Let’s pass two dictionaries to zip() to combine them together. Here is an example,


# Create dictionaries
subjects1={1: 'PHP', 2: 'Python'}
subjects2={3: 'Java', 4: 'CSS'}
print("Dictionary1:", subjects1)
print("Dictionary2:", subjects2)

# Zip the dictionaries
zip_dict = list(zip(subjects1, subjects2))
print("Zipped dictionary:", zip_dict)

# Output:
# Dictionary1: {1: 'PHP', 2: 'Python'}
# Dictionary2: {3: 'Java', 4: 'CSS'}
# Zipped dictionary: [(1, 3), (2, 4)]

Notice that it returns the keys from both dictionaries into a list of tuples. INcase if you wanted to zip the keys from the first dictionary and values from the second dictionary, use the below example.


# Zip the dictionaries
zip_dict = list(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.

3. Zip Lists into Dictionary in Python

To zip two iterable objects like lists into a Python dictionary, pass the lists as arguments and convert the result to the dictionary. This considers the elements of the first iterable as keys and the elements of the second iterable as values.


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

python zip dictionary

As we can see from the above the dictionary has been created from two lists.

4. Zip into Dictionary Using Dictionary Comprehension

Alternatively, we can use the zip() function along with dictionary comprehension to zip lists into the dictionary. 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.

python zip 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 dictionaries by combining keys from one dict and values from another dict. and learned zip lists into a dictionary. I have explained using dictionary comprehension and the dict() constructor along with zip() function.