How to map two lists into a dictionary in Python?

Easiest way is to create a zip object that returns a generator of tuples, each having an item each from two lists. The zip object can then be transformed into a dictionary by using built-in dict() function

>>> l1=['name', 'age', 'marks']
>>> l2=['Ravi', 23, 56]
>>> z=zip(l1,l2)
>>> newdict=dict(z)
>>> newdict
{'name': 'Ravi', 'age': 23, 'marks': 56}

Updated on: 30-Jul-2019


Kickstart Your Career

Get certified by completing the course

Get Started