I have a 2D jagged list/ list of lists where each entry is dictionary. I only want to change one value but when I try it I change every value in the list.
#initialise
startValue = {"a":0, "b":1, "c":2}
myList = [[startValue for i in range(3)] for j in range(3)]
print(myList)
#change one item
myList[1][1]["a"] = 5
print(myList)
The output is
[[{'a': 0, 'b': 1, 'c': 2}, {'a': 0, 'b': 1, 'c': 2}, {'a': 0, 'b': 1, 'c': 2}], [{'a': 0, 'b': 1, 'c': 2}, {'a': 0, 'b': 1, 'c': 2}, {'a': 0, 'b': 1, 'c': 2}], [{'a': 0, 'b': 1, 'c': 2}, {'a': 0, 'b': 1, 'c': 2}, {'a': 0, 'b': 1, 'c': 2}]]
[[{'a': 5, 'b': 1, 'c': 2}, {'a': 5, 'b': 1, 'c': 2}, {'a': 5, 'b': 1, 'c': 2}], [{'a': 5, 'b': 1, 'c': 2}, {'a': 5, 'b': 1, 'c': 2}, {'a': 5, 'b': 1, 'c': 2}], [{'a': 5, 'b': 1, 'c': 2}, {'a': 5, 'b': 1, 'c': 2}, {'a': 5, 'b': 1, 'c': 2}]]
Every value of "a" in the list has changed to 5. Would be very grateful to know how to change just one value. I think it is a common problem but could not find a solution.
Note that this is a dictionary in a 2D jagged list. It is not a dictionary in a dictionary nor a list in a list so is not a duplicate of these question as has been claimed and the potential solution would not be the same.