Reputation: 1365
I know this might be a very naive mistake but this has got my nerves from quite a while now.
I have a list of nested dictionaries like
grs = [{
'CS9': {
'Monday': [{
1: {
'subject': 'ESD',
'teacherName': 'Goku',
'venue': 'RN 141'
}
}, {
3: {
'subject': 'CN',
'teacherName': 'vegita',
'venue': 'RN 102'
}
}, {
5: {
'subject': 'ADA',
'teacherName': 'roshi',
'venue': 'RN 112'
}
}
]
}
}, {
'CS10': {
'Monday': [{
1: {
'subject': 'ESD',
'teacherName': 'Gohan ',
'venue': 'RN 141'
}
}, {
3: {
'subject': 'CN',
'teacherName': 'Saitama',
'venue': 'RN 102'
}
}, {
5: {
'subject': 'ADA',
'teacherName': 'Mob',
'venue': 'RN 112'
}
}
]
}
}
]
in short two groups with Monday
as a node and different events
at different slots
So, what i want to do is i want to update the Monday
node of CS10
with the data =>
tempStuff = [{'subject': 'ESD', 'teacherName': 'Aizen', 'venue': 'RN141'}, 'CS10', 2, 'Monday']
so what i do is :
for i in grs:
if tempStuff[1] in i.keys():
i[tempStuff[1]][tempStuff[3]].append({tempStuff[2]: tempStuff[0]})
What i expect to add this code to add another dictionary wiz
{
2: {
'subject': 'ESD',
'teacherName': 'Aizen',
'venue': 'RN141'
}
}
which it does but it adds this node to CS9
as well. i have tried different methods to make the data , and do the insertion in different ways, but the change is taking place in both of the nodes.
why?
any help with that?
Edit 1
There is no problem in the cropped json
i.e i have removed the data from Tuesday
to Friday
in both of the nodes for the readability of the json
in the question as the other nodes didn't matter in the insertion. So i have only mentioned Monday
.
Edit 2
Seems like the problem arises when the grs
is being created during the runtime what we have done here is that we have declared the grs by our own whereas in my code grs gets "filled" or the data insertion is happening by other loops' conditions. BUT after everything is done, grs is same as we have declared here there is no change. i.e. the grs
we get after insertion to it and the grs
we have declared here is exactly same.
Edit 3
So, here is the code for which inserts stuff in our mysterious "grs
" from another nested dict/list (FinalDict).
dataArrange = {}
for g in groups: # groups is just a list of groups eg CS10, CS9
for i in FinalDict: # the data from which i'm putting the stuff to grs
weekdet = []
for b in FinalDict[i]['lectures']:
lectdetails = []
if b[0] == 'Remedial Class':
slot = b[1]['slot']
else:
slot = b[3]['slot']
subject = b[0]
teacherName = b[1]
venue = b[2]
lectdetails = {'subject': subject, 'teacherName': teacherName, 'venue': venue}
try:
if ShortCode[lectdetails['subject']] in ElectiveStuff:
continue
except IndexError:
continue
if not lectdetails:
weekdet.append({slot: "Remedial Class"})
else:
weekdet.append({slot: lectdetails})
dataArrange.update({i: weekdet})
grs.append({g: dataArrange})
with open('stuff.txt', 'w') as g: # here we wrote grs in a file which makes me sad though it solves the problem
g.write(json.dumps(grs))
Upvotes: 1
Views: 103
Reputation: 77912
As I suspected, it's a shared reference issue. Here:
dataArrange = {}
for g in groups:
# code here
dataArrange.update({i: weekdet})
grs.append({g: dataArrange})
you are reusing dataArrange
over and over and over, so in the end each of grs[x]{g}
refer to the very same unique dict. You have to understand that Python never implicitely copies anything.
The solution is quite simple: instead of reusing the same dataArrange
dict over and over, create a new one each time.
Upvotes: 1