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
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.
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.
d = dict(zip(keys, values))
(Please don't call your dict
dict
, that's a built-in name.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With