Вы храните один и тот же экземпляр dict
, хранящийся в new['slot']
, в каждой клавише test_dict
:
from pprint import pprint
test_dict = {}
new = {}
new['slot'] = {}
new['slot']['id'] = id(new['slot'])
for k in range(5):
test_dict[k] = {}
test_dict[k].update(new)
if k == 3:
test_dict[k]['slot']['this should only be in 3'] = []
pprint(test_dict)
Выход
{0: {'slot': {'id': 4433735760, 'this should only be in 3': []}},
1: {'slot': {'id': 4433735760, 'this should only be in 3': []}},
2: {'slot': {'id': 4433735760, 'this should only be in 3': []}},
3: {'slot': {'id': 4433735760, 'this should only be in 3': []}},
4: {'slot': {'id': 4433735760, 'this should only be in 3': []}}}
Возможным исправлением будет создание нового dict
каждый раз, когда вам это нужно:
from pprint import pprint
test_dict = {}
for k in range(5):
test_dict[k] = {}
new = {'slot': dict()}
new['slot']['id'] = id(new['slot'])
test_dict[k].update(new)
if k == 3:
test_dict[k]['slot']['this should only be in 3'] = []
pprint(test_dict)
Вывод
{0: {'slot': {'id': 4399711968}},
1: {'slot': {'id': 4399712528}},
2: {'slot': {'id': 4399713088}},
3: {'slot': {'id': 4399713648, 'this should only be in 3': []}},
4: {'slot': {'id': 4399730768}}}