Update python dictionary (add another value to existing key)

前端 未结 7 1461
青春惊慌失措
青春惊慌失措 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 20:54

    you could try the below:

    d['word'] = [1, 'something']

    d['word1'] = [2, 'something']

    call d now

    Hope this helps!

    0 讨论(0)
  • 2020-12-28 20:55

    Well you can simply use:

    d['word'] = [1,'something']
    

    Or in case the 1 needs to be fetched:

    d['word'] = [d['word'],'something']
    

    Finally say you want to update a sequence of keys with new values, like:

    to_add = {'word': 'something', 'word1': 'something1'}
    

    you could use:

    for key,val in to_add.items():
        if key in d:
            d[key] = [d[key],val]
    
    0 讨论(0)
  • 2020-12-28 21:07

    You can create your dictionary assigning a list to each key

    d = {'word': [1], 'word1': [2]}
    

    and then use the following synthases to add any value to an existing key or to add a new pair of key-value:

    d.setdefault(@key,[]).append(@newvalue)
    

    For your example will be something like this:

    d = {'word': [1], 'word1': [2]}
    d.setdefault('word',[]).append('something')
    d.setdefault('word1',[]).append('something1')
    d.setdefault('word2',[]).append('something2')   
    print(d)
    {'word': [1, 'something'], 'word1': [2, 'something1'], 'word2': ['something2']}
    
    0 讨论(0)
  • 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']} 
    
    0 讨论(0)
  • 2020-12-28 21:11

    Thanks for help. I did it that way (thanks to Willem Van Onsem advice):

    x = (dict(Counter(tags))) #my dictionary
    for i in x:
        color = "#%06x" % random.randint(0, 0xFFFFFF)
        x[i] = [x[i], color]
    
    0 讨论(0)
  • 2020-12-28 21:12

    You could write a function to do this for you:

    >>> d = {'word': 1, 'word1': 2}
    >>> def set_key(dictionary, key, value):
    ...     if key not in dictionary:
    ...         dictionary[key] = value
    ...     elif type(dictionary[key]) == list:
    ...         dictionary[key].append(value)
    ...     else:
    ...         dictionary[key] = [dictionary[key], value]
    ... 
    >>> set_key(d, 'word', 2)
    >>> set_key(d, 'word', 3)
    >>> d
    {'word1': 2, 'word': [1, 2, 3]}
    

    Alternatively, as @Dan pointed out, you can use a list to save the data initially. A Pythonic way if doing this is you can define a custom defaultdict which would add the data to a list directly:

    >>> from collections import defaultdict
    >>> d = defaultdict(list)
    >>> d[1].append(2)
    >>> d[2].append(2)
    >>> d[2].append(3)
    >>> d
    defaultdict(<type 'list'>, {1: [2], 2: [2, 3]})
    
    0 讨论(0)
提交回复
热议问题