Computer >> Computer tutorials >  >> Programming >> Python

In Python how to create dictionary from two lists?


If L1 and L2 are list objects containing keys and respective values, following list comprehension syntax can be used to construct dictionary object.

>>> L1 = ['a','b','c','d']
>>> L2 = [1,2,3,4]
>>> d = {L1[k]:L2[k] for k in range(len(L1))}
>>> d
{'a': 1, 'b': 2, 'c': 3, 'd': 4}