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
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
d = dict(zip(keys, values))
(Please don't call your dict
dict
, that's a built-in name.)
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.