Reputation: 1659
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?
Upvotes: 2
Views: 28015
Reputation: 27485
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)
Upvotes: 2
Reputation: 11728
TL;DR: You can append
a value to a Python list (or array), but not to a dictionary.
key: value
pair to dictionaryThe 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
key: value
pairsIf 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.
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.
Upvotes: 5
Reputation: 72
if key in dictionary:
dictionary[key] = dictionary[key] + message1
print(dictionary[key])
else:
dictionary[key] = message1
Upvotes: 0