Convert Two Lists into a Dictionary in Python

Converting two lists into a dictionary is a breeze in Python when using the dict and zip methods! Additionally, there are alternatives.

Using Zip & Dict

>>> students = ["Cody", "Ashley", "Kerry"]
>>> grades = [93.5, 95.4, 82.8]
>>> dict(zip(students, grades))
{'Cody': 93.5, 'Ashley': 95.4, 'Kerry': 82.8}

Dictionary Comprehension

>>> students = ["Cody", "Ashley", "Kerry"]
>>> grades = [93.5, 95.4, 82.8]
>>> {s: g for s, g in zip(students, grades)}
{'Cody': 93.5, 'Ashley': 95.4, 'Kerry': 82.8}

Python <= 2.6

>>> from itertools import izip
>>> students = ["Cody", "Ashley", "Kerry"]
>>> grades = [93.5, 95.4, 82.8]
>>> dict(izip(keys, values))
{'Cody': 93.5, 'Ashley': 95.4, 'Kerry': 82.8}