How to Create a Dictionary from Two Lists in Python?

As a data scientist working with US demographic data, I recently faced a challenge where I had two separate lists – one containing state names and the other containing their respective population counts. In this tutorial. To make the data more manageable and accessible, I needed to combine these lists into a single dictionary. I will explain how to create a dictionary from two lists in Python with examples.

Create a Dictionary from Two Lists in Python

Let’s say we have two lists:

states = ['California', 'Texas', 'Florida', 'New York', 'Illinois']
populations = [39538223, 29145505, 21538187, 20201249, 12812508]

Our goal is to create a dictionary where the state names serve as keys and the population counts serve as corresponding values, like this:

state_population = {
    'California': 39538223,
    'Texas': 29145505,
    'Florida': 21538187,
    'New York': 20201249,
    'Illinois': 12812508
}

Read How to Check if a Key Exists in a Python Dictionary?

Method 1: Use a For Loop

One simple approach is to use a for loop to iterate over the lists and create key-value pairs in the Python dictionary. Here’s how you can do it:

state_population = {}
for state, population in zip(states, populations):
    state_population[state] = population
print(state_population)

Output:

{'California': 39538223, 'Texas': 29145505, 'Florida': 21538187, 'New York': 20201249, 'Illinois': 12812508}

You can see the output in the screenshot below.

Create a Dictionary from Two Lists in Python

In this code snippet, we initialize an empty dictionary called state_population. We then use the zip() function to pair up the elements from the states and populations lists. The for loop iterates over each pair, assigning the state name as the key and the population count as the value in the Python dictionary.

Check out How to Add Items to a Dictionary in Python?

Method 2: Use Dictionary Comprehension

Python offers a concise way to create Python dictionaries using dictionary comprehension. Here’s how you can use it to create a dictionary from two lists:

state_population = {state: population for state, population in zip(states, populations)}
print(state_population)

Output:

{'California': 39538223, 'Texas': 29145505, 'Florida': 21538187, 'New York': 20201249, 'Illinois': 12812508}

You can see the output in the screenshot below.

How to Create a Dictionary from Two Lists in Python

This one-liner achieves the same result as the for loop method. It creates key-value pairs by iterating over the zipped lists and assigning the state name as the key and the population count as the value.

Read How to Sort a Dictionary by Value in Python?

Handle Lists of Different Lengths

In the examples above, we assumed that the states and populations lists have the same length. But what if they don’t? In such cases, we can use the zip_longest() function from the itertools module to handle lists of different lengths. Here’s an example:

from itertools import zip_longest

states = ['California', 'Texas', 'Florida']
populations = [39538223, 29145505, 21538187, 20201249, 12812508]

state_population = {state: population for state, population in zip_longest(states, populations, fillvalue=None)}

Output:

{'California': 39538223, 'Texas': 29145505, 'Florida': 21538187, None: 12812508}

You can see the output in the screenshot below.

Create a Dictionary from Two Lists in Python Lists of Different Lengths

In this case, if one list is shorter than the other, the zip_longest() function will fill in None values for the missing elements. You can specify a different fill value using the fillvalue parameter.

Check out How to Remove an Item from a Dictionary in Python?

Conclusion

In this article, I helped you to learn how to create a dictionary from two lists in Python. I discussed two methods to do this task such as using a for loop and using a dictionary comprehension. I also discussed how to handle lists of different lengths.

You can also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.