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}

Updated on: 30-Jul-2019

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements