In Python how to create dictionary from two lists?

PythonServer Side ProgrammingProgramming

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}
raja
Published on 08-Feb-2018 23:39:54
Advertisements