Update python dictionary (add another value to existing key)

前端 未结 7 1498
青春惊慌失措
青春惊慌失措 2020-12-28 20:38

I have simple dictionary with key, value:

d = {\'word\': 1, \'word1\': 2}

I need to add another value (to make a list from values):

7条回答
  •  甜味超标
    2020-12-28 21:10

    This is what I would do. I hope it helps. If you have a lot of items to add to your dictionary this may not be the fastest route.

    d = {'word':1, 'word1':2}
    
    d['word']=[1, 'something']
    
    d['word1']=[2, 'something1']
    
    print(d)
    
    {'word': [1, 'something'], 'word1': [2, 'something1']} 
    

提交回复
热议问题