Convert Two Lists

Convert Two Lists Into a Dictionary

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++'}

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++'}

Reference : https://www.programiz.com/python-programming/examples/list-to-dictionary

Last updated