I've many lists holding many dictionaries inside a list. I simply assign to one dictionary in one of the lists in the outer list. But it leads to assignment to all dictionaries in all lists in the outer list.
The code:
CL=3*[0]
DL=4*[0]
di= {
'A':[],
'B':[],
'C':CL,
'D':DL
}
R=[[],[]]
R[0].append(di)
R[1].append(di)
def func(dd):
dd[0][0]['A'].append("BANANA")
dd[0][0]['B'].append("ELEPHANT")
dd[0][0]['C'][0]='BLUE'
dd[0][0]['D'][3]='ROCK'
dd[0][0]['D'][2]=1111
print(R[0])
print(R[1])
print("\n")
func(R)
print(R[0])
print(R[1])
The output:
[{'A': [], 'C': [0, 0, 0], 'B': [], 'D': [0, 0, 0, 0]}]
[{'A': [], 'C': [0, 0, 0], 'B': [], 'D': [0, 0, 0, 0]}]
[{'A': ['BANANA'], 'C': ['BLUE', 0, 0], 'B': ['ELEPHANT'], 'D': [0, 0, 1111, 'ROCK']}]
[{'A': ['BANANA'], 'C': ['BLUE', 0, 0], 'B': ['ELEPHANT'], 'D': [0, 0, 1111, 'ROCK']}]
As you can see, even though I assigned values to the dictionary in the first list in the outer list (func() operates only on dd[0]..), both the lists got assigned.
Is there any mistake in my indexing anywhere? Why does this happen?