I am new to Python and I am trying to insert values into specific dictionary keys in a list. However, they all share the same key name and when I try to insert a value, all the keys with the same name in the different dictionaries are also updated.
Here's a simple example of what I am using:
name = "Jacob`Mary`Anne"
age = "20`34`31"
text = "Text1`Text2`Text3"
def compile():
dict = {"name": "", "age": "", "text": ""}
full_dict = []
for i in range(3):
full_dict.append(dict)
return {
"date": "",
"content": [full_dict]
}
response = compile()
name_split = name.split('`')
age_split = age.split('`')
text_split = text.split('`')
for i in range(3):
response[content][0][i]['name'] = name_split[i]
response[content][0][i]['age'] = age_split[i]
response[content][0][i]['text'] = text_split[i]
I am trying to get an output that looks like this:
[[{'name':'Jacob', 'age':'20', 'text':'Text1'}, {'name':'Mary', 'age':'34', 'text':'Text2'}, {'name':'Anne', 'age':'31', 'text':'Text3'}]]
However, I end up with an output like this:
[[{'name':'Anne', 'age':'31', 'text':'Text3'}, {'name':'Anne', 'age':'31', 'text':'Text3'}, {'name':'Anne', 'age':'31', 'text':'Text3'}]]
I'd appreciate any help I can get. Thanks!