Convert two Lists into a Dictionary in Python

Here are two ways to convert two lists into a dictionary in Python:

(1) Using zip() and dict():

list_keys = ["item_1", "item_2", "item_3", ...]
list_values = ["item_1", "item_2", "item_3", ...]

new_dict = dict(zip(list_keys, list_values))

(2) Using a Dictionary Comprehension:

list_keys = ["item_1", "item_2", "item_3", ...]
list_values = ["item_1", "item_2", "item_3", ...]

new_dict = {k: v for k, v in zip(list_keys, list_values)}

Examples of Converting two Lists into a Dictionary in Python

Example 1: Using zip() and dict()

Suppose that you have the following two lists that you’d like to convert into a dictionary:

list_keys = [1, 2, 3, 4, 5]
list_values = ["blue", "green", "red", "yellow", "orange"]

Here is the complete syntax to convert the two lists into a new dictionary:

list_keys = [1, 2, 3, 4, 5]
list_values = ["blue", "green", "red", "yellow", "orange"]

new_dict = dict(zip(list_keys, list_values))

print(new_dict)
print(type(new_dict))

You’ll then get the following dictionary:

{1: 'blue', 2: 'green', 3: 'red', 4: 'yellow', 5: 'orange'}
<class 'dict'>

Note that print(type(new_dict)) was added at the bottom of the code to demonstrate that you got a dictionary.

Example 2: Using a Dictionary Comprehension

Alternatively, you can convert the two lists into a dictionary using a Dictionary Comprehension:

list_keys = [1, 2, 3, 4, 5]
list_values = ["blue", "green", "red", "yellow", "orange"]

new_dict = {k: v for k, v in zip(list_keys, list_values)}

print(new_dict)
print(type(new_dict))

You’ll get the same dictionary as before:

{1: 'blue', 2: 'green', 3: 'red', 4: 'yellow', 5: 'orange'}
<class 'dict'>

Leave a Comment