+2 votes
in Programming Languages by (73.8k points)
How can I create a dictionary using two lists? The elements of one list should be keys and the elements of another list should be values of the dictionary.

e.g.

a=['a','b','c','d','e']

b=[1,2,3,4,5]

The output should be:

{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

1 Answer

+2 votes
by (348k points)
selected by
 
Best answer

You can combine dict() and zip() to convert lists to a dictionary.

Here is an example:

>>> a=['a','b','c','d','e']
>>> b=[1,2,3,4,5]
>>> dict(zip(a,b))
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
 


...