Sorry if this is a basic Python question, but for some reason, I can't figure this out.
Suppose I create a list of a list of a dictionary,
b=[[{'j':1}]*3]*3
so that "b" is this:
[[{'j': 1}, {'j': 1}, {'j': 1}],
[{'j': 1}, {'j': 1}, {'j': 1}],
[{'j': 1}, {'j': 1}, {'j': 1}]]
Now suppose I want to change "b" to:
[[{'j': 1}, {'j': 1}, {'j': 1}],
[{'j': 1}, {'j': 1}, {'j': 90}],
[{'j': 1}, {'j': 1}, {'j': 1}]]
so I naively use this line of code
b[1][2]['j']=90
However, "b" is now this:
[[{'j': 90}, {'j': 90}, {'j': 90}],
[{'j': 90}, {'j': 90}, {'j': 90}],
[{'j': 90}, {'j': 90}, {'j': 90}]]
Why did all of the dictionaries in the list of a list change? Why didn't only the b[1][2] entry change?
Also, the lines
b=[[{'j':1}]*3]*3
b[1][2]={'j':90}
changes "b" to
[[{'j': 1}, {'j': 1}, {'j': 90}],
[{'j': 1}, {'j': 1}, {'j': 90}],
[{'j': 1}, {'j': 1}, {'j': 90}]]
which I also can't explain.