The following demo code:
mydict = {}
mylist = []
mydict["s"] = 1
mydict["p"] = "hasprice"
mydict["o"] = 3
print(mydict)
mylist.append(mydict)
mydict["s"] = 22
mydict["p"] = "hasvat"
mydict["o"] = 66
print(mydict)
mylist.append(mydict)
print(mylist)
prints out the following result:
[{'s': 22, 'p': 'hasvat', 'o': 66}, {'s': 22, 'p': 'hasvat', 'o': 66}]
and the only explanation that comes to my mind is that mydict is assigned by reference and therefore the list items all point to a same memory object. Is this the reason?
How can I properly append multiple different dictionaries to the list?
I am building each mydict dictionary within a loop and then wanted to append it to the list which I will finally write to a JSON file.