How to create a dictionary from two lists in Python

Creating a dictionary from two lists is a common task in Python, often encountered in various programming scenarios. The dictionary is a versatile data structure that allows us to store key-value pairs efficiently. A list is a built-in data structure representing an ordered element collection.

List vs. dictionary
List vs. dictionary

In this Educative Answer, we’ll explore nine different methods for transforming two lists into a dictionary. Whether we’re beginners or experienced Python developers, understanding these methods will broaden our toolkit and help us work more effectively with data. We’ll walk through each method step by step, providing code examples and explanations.

Methods

Let’s dive in and explore these methods in detail.

1. Using a loop

The simplest way to create a dictionary from two lists is by using a loop. Here’s how we can do it:

keys = ["key1", "key2", "key3", "key4", "key5", "key6"]
values = [1, 2, 3, 4, 5, 6]
result_dict = {}
for i in range(len(keys)):
result_dict[keys[i]] = values[i]
print(result_dict)

In this method, we iterate through the indexes of the keys list and use them to access corresponding elements from the values list, building the dictionary as we go.

2. Using the zip() function

Python provides a handy zip() function that allows us to combine two lists element wise and create a dictionary in a more concise way. Here’s how we can do it:

keys = ["key1", "key2", "key3", "key4", "key5", "key6"]
values = [1, 2, 3, 4, 5, 6]
result_dict = dict(zip(keys, values))
print(result_dict)

The zip function pairs elements from the keys and values lists together, and the dict constructor converts these pairs into key-value pairs in a dictionary.

3. Using the dict() constructor with key-value pairs

We can create a list of key-value pairs and then pass it to the dict() constructor to build a dictionary. Here’s how we can do it:

keys = ["key1", "key2", "key3", "key4", "key5", "key6"]
values = [1, 2, 3, 4, 5, 6]
key_value_pairs = [(keys[i], values[i]) for i in range(len(keys))]
result_dict = dict(key_value_pairs)
print(result_dict)

Here, we create a list of tuples where each tuple contains a key from the keys list and a corresponding value from the values list.

4. Using a loop with the enumerate() function

Another approach is to use the enumerate() function to loop through both lists simultaneously. Here’s how we can do it:

keys = ["key1", "key2", "key3", "key4", "key5", "key6"]
values = [1, 2, 3, 4, 5, 6]
result_dict = {}
for index, key in enumerate(keys):
result_dict[key] = values[index]
print(result_dict)

The enumerate() function provides both the index and the value from the keys list, simplifying the dictionary creation process.

5. Using dictionary comprehension with the zip() function

We can achieve the same result as Method 2 using dictionary comprehension. Here’s how we can do it:

keys = ["key1", "key2", "key3", "key4", "key5", "key6"]
values = [1, 2, 3, 4, 5, 6]
result_dict = {k: v for k, v in zip(keys, values)}
print(result_dict)

Dictionary comprehension is more concise and Pythonic, especially when we have short lists.

6. Using collections.OrderedDict with the zip() function

If we need to maintain the order of elements in the resulting dictionary, we can use OrderedDict along with the zip() function. Here’s how we can do it:

from collections import OrderedDict
keys = ["key1", "key2", "key3", "key4", "key5", "key6"]
values = [1, 2, 3, 4, 5, 6]
result_dict = OrderedDict(zip(keys, values))
print(result_dict)

OrderedDict preserves the order of elements, which can be essential in certain cases.

7. Using a loop with the zip() and iter() functions

We can also utilize the iter() function to create a dictionary. Here’s how we can do it:

keys = ["key1", "key2", "key3", "key4", "key5", "key6"]
values = [1, 2, 3, 4, 5, 6]
it = iter(values)
result_dict = {key: next(it) for key in keys}
print(result_dict)

Here, we use an iterator to get values one by one, creating the dictionary in the process.

8. Using a loop with the zip() and setdefault() functions

We can build a dictionary by using the setdefault() method within a loop. Here’s how we can do it:

keys = ["key1", "key2", "key3", "key4", "key5", "key6"]
values = [1, 2, 3, 4, 5]
result_dict = {}
for k, v in zip(keys, values):
result_dict[k] = v
# Set a default value for keys missing in the values list
default_value = None
for k in keys:
result_dict.setdefault(k, default_value)
print(result_dict)

This method sets default values for keys that don’t exist in the dictionary.

9. Using a loop with the zip() and defaultdict() functions

For more advanced cases, we can use the defaultdict() method from the collections library to create a dictionary with default values. Here’s how we can do it:

from collections import defaultdict
keys = ["key1", "key2", "key3", "key4", "key5", "key6"]
values = [1, 2, 3, 4, 5, 6]
result_dict = defaultdict(lambda: None, zip(keys, values))
print(result_dict)
print(result_dict["key7"]) # This will print: None

The defaultdict() function allows us to specify a default value for missing keys.

Here’s a table summarizing when and how to use different methods to create a dictionary from two lists in Python:

Method

Description

When to Use

1. Using a loop

Iterate through the lists using a loop and manually populate the dictionary.

When you need fine-grained control over the creation process, or when additional logic is required during iteration.

2. Using the zip() function

Use the zip() function to combine the two lists and then create a dictionary from the resulting pairs.

When the lists are of equal length and you want a concise way to pair corresponding elements from the two lists.

3. Using the dict() constructor

Use the dict() constructor with key-value pairs created from the two lists.

When you want a simple, one-liner approach to create a dictionary from two lists with a clear syntax.

4. Using a loop with the enumerate() function

Use a loop with the enumerate() function to iterate over one list while using the index to access elements from the other list.

When you need both the values and indexes from one of the lists during dictionary creation.

5. Using a dictionary comprehension with the zip() function

Utilize a dictionary comprehension with the zip function to create key-value pairs in a concise and readable way.

When you prefer a concise and expressive one-liner for creating dictionaries, especially when the logic is straightforward.

6. Using OrderedDict with the zip() function

Use collections.OrderedDict with the zip() function if the order of the elements in the dictionary needs to be maintained.

When the order of the elements in the resulting dictionary matters, and you want to ensure that it reflects the order in the input lists.

7. Using a loop with the zip() and iter() functions

Combine the zip fucntion with the iter() function to create pairs and iterate through them using a loop.

When you need to customize the iteration process, or when you want to handle pairs sequentially in a more granular way.

8. Using a loop with the zip() and setdefault() functions

Use a loop with the zip() and setdefault() functions to set default values for missing keys.

When you want to set default values for keys in the resulting dictionary, especially when handling missing values is important.

9. Using a loop with the zip() and defaultdict() functions

Use collections.defaultdict() with zip to set a default factory for missing keys.

When you want to set a default value for missing keys and prefer using the convenient features of the defaultdict() function.

Conclusion

In Python, the transformation of two lists into a dictionary is a fundamental operation, and our exploration of nine distinct methods has shed light on the versatility and flexibility that Python offers. Each method brings its own strengths, from the simplicity of loops and the conciseness of dictionary comprehensions to the precision of the OrderedDict() function and the advanced capabilities of the defaultdict() function. As a Python programmer, having this array of tools at our disposal allows us to tailor our approach to the specific demands of our projects. Whether we prioritize clarity, efficiency, or uniqueness, understanding these techniques equips us to navigate the world of dictionaries with confidence, making our Python code more powerful and adaptable to a variety of scenarios.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved