Create a Dictionary from Two Separate Lists in Python

Creating a dictionary from two separate lists allows you to combine related elements as key-value pairs. In this blog post, we will explore different methods to create a dictionary from two separate lists in Python.

Method 1: Using the zip() function

keys = ["a", "b", "c"]
values = [1, 2, 3]
my_dict = dict(zip(keys, values))

print("Dictionary:", my_dict)

Output:

Dictionary: {‘a’: 1, ‘b’: 2, ‘c’: 3}

Method 2: Using a dictionary comprehension

keys = ["a", "b", "c"]
values = [1, 2, 3]
my_dict = {keys[i]: values[i] for i in range(len(keys))}

print("Dictionary:", my_dict)

Output:

Dictionary: {‘a’: 1, ‘b’: 2, ‘c’: 3}