-1

here's my code:

import copy
    
teamdict = {1:{},2:{},3:{},4:{},5:{}}
teamlst = []
matchlst = [1,2,3,4,5]
lst = [[1,5,0],[1,0,3],[2,3,0],[2,0,1],[3,0,6],[3,0,1],[4,0,1],[4,0,5],[5,0,8]]
for i in matchlst:
    for j in lst:
        if i == j[0]:
            teamlst.append(j[2])
        elif len(teamlst)>0 and i != j:
            continue
        else:
            pass
    teamlst = set(teamlst)
    teamlst = list(teamlst)
    teamlst.sort()
    team_dictionary = dict.fromkeys(teamlst, [])
    teamdict[i] = team_dictionary
    teamlst.clear()
print(teamdict)
dic = copy.deepcopy(teamdict)

for i in lst:
    dic[i[0]][i[2]].append(i[1])
print(dic)

this is what i got:

{1: {0: [5, 0], 3: [5, 0]}, 2: {0: [3, 0], 1: [3, 0]}, 3: {1: [0, 0], 6: [0, 0]}, 4: {1: [0, 0], 5: [0, 0]}, 5: {8: [0]}}

but what i expect is:

{1: {0: [5], 3: [0]}, 2: {0: [3], 1: [0]}, 3: {1: [0], 6: [0]}, 4: {1: [0], 5: [0]}, 5: {8: [0]}}

what goes wrong? seems like this append method add the element to all the keys in the nested dictionary

  • 1
    It is hard to guess what your code should do. Could you add an explanation of what you are trying to do here? – Lexpj Jan 21 '23 at 10:40
  • so basically I want to add a value from the list, which is i[1] to a corresponding nested dictionary based on the i[0] and i[2] – user21054862 Jan 21 '23 at 10:43
  • 4
    it does the expected result. Voting to close – RomanPerekhrest Jan 21 '23 at 10:45
  • Welcome to Stack Overflow. `dict.fromkeys(teamlst, [])` makes each value in the dict be **the same** empty list; so adding to it means the change is reflected in each value. Please see the linked duplicates for details. – Karl Knechtel Jan 21 '23 at 11:50
  • (For future questions, please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and [mre] and try to figure out exactly what part of the code is *actually causing a problem*. – Karl Knechtel Jan 21 '23 at 12:08

1 Answers1

0

I assume you have created your dictionary with multiple references to the same list.

If I execute your code I get what you want.

>>> lst = [[1,5,0],[1,0,3],[2,3,0],[2,0,1],[3,0,6],[3,0,1],[4,0,1],[4,0,5],[5,0,8]]
>>> dic = {1: {0: [], 3: []}, 2: {0: [], 1: []}, 3: {1: [], 6: []}, 4: {1: [], 5: []}, 5: {8: []}}
>>> for i in lst:
...     dic[i[0]][i[2]].append(i[1])
...
>>> print(dic)
{1: {0: [5], 3: [0]}, 2: {0: [3], 1: [0]}, 3: {1: [0], 6: [0]}, 4: {1: [0], 5: [0]}, 5: {8: [0]}}
Ruben
  • 38
  • 1
  • 6
  • yes you are right. what should i do if I created such a dictionary with multiple references? – user21054862 Jan 21 '23 at 11:11
  • i tried to use deepcopy but it doesn't work – user21054862 Jan 21 '23 at 11:32
  • 1
    " what should i do if I created such a dictionary with multiple references?" If they need to be separate, then **don't** create that; make the dictionary start off with separate empty lists instead. Copying, even deep copy, cannot help here, because the copy will have the *same aliasing structure* with its own distinct group of objects. – Karl Knechtel Jan 21 '23 at 12:09