Join our newsletter for the latest updates.

Python Program to Convert Two Lists Into a Dictionary

In this example, you will learn to convert two lists into a dictionary.

To understand this example, you should have the knowledge of the following Python programming topics:


Example 1: Using zip and dict methods

index = [1, 2, 3]
languages = ['python', 'c', 'c++']

dictionary = dict(zip(index, languages))
print(dictionary)

Output

{1: 'python', 2: 'c', 3: 'c++'}

The zip() function takes iterables (can be zero or more), aggregates them in a tuple, and returns it. Likewise, dict() gives the dictionary.


Example 2: Using list comprehension

index = [1, 2, 3]
languages = ['python', 'c', 'c++']

dictionary = {k: v for k, v in zip(index, languages)}
print(dictionary)

Output

{1: 'python', 2: 'c', 3: 'c++'}
Did you find this article helpful?