Update python dictionary (add another value to existing key)

前端 未结 7 1462
青春惊慌失措
青春惊慌失措 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:16

    It will be easiest if you always use lists, even when you just have a single value. It will just be a list of length 1.

    >>> d = {'a': [1], 'b': [2]}
    >>> d
    {'a': [1], 'b': [2]}
    >>>
    >>> d['a'].append(5)
    >>> d
    {'a': [1, 5], 'b': [2]}
    
    0 讨论(0)
提交回复
热议问题