简体   繁体   中英

How to add new values to existing dictionary in python

I am trying to create a python dictionary that has a collection of keys with multiple values per key. I want to be able to add values to an existing key in the dictionary. I have reviewed multiple threads on this but I have not found one that applies directly.

Here is format of the key

{ "key1":{"value1-1 Attr": "value1-1","value1-2 Attr": "value1-2","value1-3 Attr": "value1-3", "Key2":{"value2-1 Attr": "value2-1","value2-2 Attr": "value2-2","value2-3 Attr": "value2-3",{} } 

I want to be able to add new keys and also increase the value of existing key so I tried this Python code:

message1 = {"value1-4 Attr": "value1-4","value1-5 Attr": "value1-6","value1-7 Attr": "value1-7"}

if key in dictionary:
   dictionary[key].append(message1)
else:
    dictionary[key] =message1

The plan is to write the dictionary to a json file later. The problem is that I keep getting this error, which I'm not sure how to solve:

'dict' object has no attribute 'append'

How could I fix the error and what is the best way to implement adding new value to existing key?

You should be using a list to store these values. There's no reason to have nested dicts here. Especially since you're just using append here.

Your data would then look like this:

data = {
  "key1":["value1-1", "value1-2","value1-3"],
  "Key2":["value2-1","value2-2", "value2-3"]}

Then you won't need an if statement just use dict.setdefault

data.setdefault(key, []).append(message)

TL;DR: You can append a value to a Python list (or array), but not to a dictionary.


Add key: value pair to dictionary

The syntax dictionary[key] = message1 changes the existing value of dictionary[key] to message if the key is already in the dictionary, and creates a key-value pair key: message1 if the key is not yet in the dictionary.

So instead of this...

if key in dictionary:
    dictionary[key].append(message1)
else:
    dictionary[key] = message1

...you would use this:

dictionary[key] = message1

Update dictionary with different key: value pairs

If you want to update a dictionary with the values from another dictionary, you can do it this way:

dictionary_1.update(dictionary_2)

This modifies dictionary_1 in place, using the values for each key in dictionary_2 .

If a key does not exist in dictionary_1 , but exists in dictionary_2 , then update will modify dictionary_1 based on dictionary_2 , so that dictionary_1 includes the key-value pair key: dictionary_2[key] .

Or, if a key exists in both dictionary_1 and dictionary_2 , then update will overwrite the existing value in dictionary_1[key] with the value from dictionary_2[key] .

So instead of this...

if key in dictionary:
    dictionary[key].append(message1)
else:
    dictionary[key] = message1

...you would use this:

dictionary[key].update(message1)

This works only if the value of dictionary[key] is a dictionary.


Append values to list within dictionary

If you want a key to have multiple values, you can store the multiple values in a list:

dictionary = {key: [value_1, value_2, value_3]}

Then you can append another value to the list:

dictionary[key].append(value_4)

Result:

dictionary = {key: [value_1, value_2, value_3, value_4]}

So instead of this...

if key in dictionary:
    dictionary[key].append(message1)
else:
    dictionary[key] = message1

...you would use this:

if key in dictionary:
    dictionary[key].append(message1)
else:
    dictionary[key] = [message1]

If key already exists in dictionary , this appends message to dictionary[key] . Otherwise, it creates a new single-item list [message1] as the value of dictionary[key] .

However, dictionary[key].append(message1) only works if the value of dictionary[key] is a list, not if the value is a dictionary.

if key in dictionary:
    dictionary[key] = dictionary[key] + message1
    print(dictionary[key])
else:
    dictionary[key] = message1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM