简体   繁体   中英

create a dictionary in python with a key and a list of values in python

I have the dictionary

list = ["1", "2", "3"]

and a single key. now i want to form a dictionary like:

dic = {"key": "1", "key": "2", "key": "3"}

Does anyone know how to do it?


I have tried:

list = ["1", "2", "3"]

dic = {}

for value in list:
    dic['key'] = value
    
print(dic)

But it returns:

{'key': '3'}

thanks, Marius.

Dictionary, by definition , can't have the same key twice.

Consider storing the values under the same key, using other kind of container, such as list, set or tuple:

{"key": ["1", "2", "3"]}

It's not possible, Python does not allow duplicate keys inside a dictionary. Keys act as unique identifiers thus Duplicate keys might lead to ambiguity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM