adding new values in a typical key in a dictionary

前端 未结 2 489
执笔经年
执笔经年 2021-01-28 17:36

how do I add value to another dictionary to the same key like below

con = {\'a\':{\'b\':\'c\'}, b:{\'d\':\'e\'}} 

into

con = {         


        
2条回答
  •  遇见更好的自我
    2021-01-28 17:48

    With the current set up, its always a key and a value pair, so it will be key:value

    If you would like to have more than one value to a key, please use.

    from collections import defaultdict
    myDict = defaultdict(list)
    

    Now, you can add more than one value to the key.

    myDict[key1].append(keyA:Value)
    myDict[key1].append(keyB:Value)
    

    Hope this helps.

    Cheers!

提交回复
热议问题