initialize dict with keys,values from two list

Question:

I have read this link

But how do I initialize the dictionary as well ?

say two list

 keys = ['a','b','c','d'] 
 values = [1,2,3,4] 
 dict = {}

I want initialize dict with keys & values

Asked By: Jibin

||

Answers:

d = dict(zip(keys, values))

(Please don’t call your dict dict, that’s a built-in name.)

Answered By: Fred Foo

In Python 2.7 and python 3, you can also make a dictionary comprehension

d = {key:value for key, value in zip(keys, values)}

Although a little verbose, I found it more clear, as you clearly see the relationship.

Answered By: Khelben
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.