How to map two lists into a dictionary in Python?

PythonServer Side ProgrammingProgramming

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}
Published on 22-Feb-2018 10:14:44