adding new values in a typical key in a dictionary

前端 未结 2 444
执笔经年
执笔经年 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 18:00

    With the problem as stated, there is no reason you cannot use direct key-value assignment:

    con = {'a':{'b':'c'}, 'b':{'d':'e'}} 
    
    con['a']['e'] = 'f'
    
    print(con)
    
    {'a': {'b': 'c', 'e': 'f'}, 'b': {'d': 'e'}}
    

    Notice we can chain dictionary keys. This is natural because con['a'] returns a dictionary, which can itself be assigned a new key-value pair.

提交回复
热议问题