Recently in a Python webinar, someone asked me how to zip a dictionary in Python, which became the discussion topic. After researching various methods to accomplish this task, I found a few important techniques. In this tutorial, I will share my findings along with suitable examples and screenshots of executed example code.
zip a dictionary in Python
In Python, the zip() function is a useful tool for combining multiple iterables, such as lists or dictionaries, by aggregating elements based on their positions. When working with dictionaries, zip() can be utilized to create new dictionaries by pairing keys and values from separate iterables.
Read How to Create a Dictionary from Two Lists in Python?
1. Create a Dictionary from Two Lists Using zip()
Suppose you have two Python lists: one containing state names and another containing their capitals. You can combine these lists into a dictionary where each state is mapped to its capital.
# List of U.S. states
states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas']
# Corresponding list of capitals
capitals = ['Montgomery', 'Juneau', 'Phoenix', 'Little Rock']
# Creating a dictionary using zip()
state_capital_dict = dict(zip(states, capitals))
print(state_capital_dict)Output:
{'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix', 'Arkansas': 'Little Rock'}You can refer to the below screenshot to see the output:

In this example, zip(states, capitals) pairs each state with its capital, and dict() constructs the dictionary from these pairs.
Check out How to Check if a Key Exists in a Python Dictionary?
2. Create a Dictionary from Two Dictionaries Using zip()
If you have two Python dictionaries and wish to combine them into a single dictionary, you can use zip() to pair their keys and values.
# Dictionary of U.S. states and their abbreviations
state_abbreviations = {
'Alabama': 'AL',
'Alaska': 'AK',
'Arizona': 'AZ',
'Arkansas': 'AR'
}
# Dictionary of abbreviations and their populations (in millions)
state_populations = {
'AL': 4.9,
'AK': 0.7,
'AZ': 7.3,
'AR': 3.0
}
# Creating a dictionary by zipping values from both dictionaries
combined_dict = {state: state_populations[abbr] for state, abbr in state_abbreviations.items()}
print(combined_dict)Output:
{'Alabama': 4.9, 'Alaska': 0.7, 'Arizona': 7.3, 'Arkansas': 3.0}You can refer to the below screenshot to see the output:

Here, we iterate over state_abbreviations and map each state to its population by accessing state_populations using the abbreviation.
Read How to Add Items to a Dictionary in Python?
3. Create a Dictionary with Enumerated Keys Using zip()
When you have a list of items and want to create a dictionary with indices as keys, you can use zip() in combination with range() in Python.
# List of U.S. presidents
presidents = ['George Washington', 'John Adams', 'Thomas Jefferson', 'James Madison']
# Creating a dictionary with indices as keys
president_dict = dict(zip(range(1, len(presidents) + 1), presidents))
print(president_dict)Output:
{1: 'George Washington', 2: 'John Adams', 3: 'Thomas Jefferson', 4: 'James Madison'}You can refer to the below screenshot to see the output:

In this case, range(1, len(presidents) + 1) generates indices starting from 1, and zip() pairs each index with a president’s name.
Read How to Sort a Dictionary by Value in Python?
4. Create a Dictionary from a List of Tuples Using zip()
If you have a list of tuples in Python, where each tuple contains a key-value pair, you can unzip this list into two separate lists and then combine them back into a dictionary.
# List of tuples containing state and capital pairs
state_capital_pairs = [
('California', 'Sacramento'),
('Texas', 'Austin'),
('Florida', 'Tallahassee'),
('New York', 'Albany')
]
# Unzipping into two lists
states, capitals = zip(*state_capital_pairs)
# Creating a dictionary using zip()
state_capital_dict = dict(zip(states, capitals))
print(state_capital_dict)Output:
{'California': 'Sacramento', 'Texas': 'Austin', 'Florida': 'Tallahassee', 'New York': 'Albany'}Here, zip(*state_capital_pairs) unpacks the list of tuples into two separate tuples, which are then zipped back together into a dictionary.
Check out How to Remove an Item from a Dictionary in Python?
Handle Unequal Lengths with zip_longest()
When dealing with iterables of unequal lengths, the zip() function stops at the shortest iterable. To handle this and fill a missing values, you can use zip_longest() from the itertools module.
from itertools import zip_longest
# List of U.S. states
states = ['California', 'Texas', 'Florida']
# Corresponding list of capitals (one missing)
capitals = ['Sacramento', 'Austin']
# Creating a dictionary using zip_longest() with a default value
state_capital_dict = dict(zip_longest(states, capitals, fillvalue='Unknown'))
print(state_capital_dict)Output:
{'California': 'Sacramento', 'Texas': 'Austin', 'Florida': 'Unknown'}In this example, zip_longest() pairs elements from both lists and fills the missing capital with 'Unknown' for the state of Florida.
Read How to Initialize a Dictionary in Python?
Conclusion
In this tutorial, I have helped you to understand how to zip a dictionary in Python. I explained creating a dictionary from two lists using zip(), creating a dictionary from two dictionaries using zip(), creating a dictionary with enumerated keys using zip(), creating a dictionary from a list of tuples using zip(), handling unequal lengths with zip_longest().
You can also read:
- How to Sort a Python Dictionary by Key Alphabetically?
- How to Print a Dictionary in Python?
- How to Get Keys of a Dictionary in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.