Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initialize dict with keys,values from two list [duplicate]

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

like image 592
Jibin Avatar asked Nov 10 '11 14:11

Jibin


People also ask

Can dictionary have duplicate keys with different values?

You can not have duplicate keys in Python, but you can have multiple values associated with a key in Python. If you want to keep duplicate keys in a dictionary, you have two or more different values that you want to associate with same key in dictionary.

Can a dictionary have two keys with the same value two values with the same key?

No, each key in a dictionary should be unique. You can't have two keys with the same value. Attempting to use the same key again will just overwrite the previous value stored. If a key needs to store multiple values, then the value associated with the key should be a list or another dictionary.


1 Answers

d = dict(zip(keys, values)) 

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

like image 197
Fred Foo Avatar answered Nov 06 '22 17:11

Fred Foo