0

I want to assign a value from a dictionary into another but when I do that it seems to point to the original dictionary and both get changed when I make modifications. Note I only want certain values so I cannot just copy the whole dictionary (unless I am mistaken).

Originaldic = {"Foo":["Somthing",1], "Bar":["action",2], "baz":["otherthing",6]}
listofplayer = ["Bar","Foo"]
Tempdic = {}
for player in listofplayer:
    Tempdic[player]=Originaldic[player]
    #the problem is likely at this step above

    if Tempdic[player][1]==2:
        Tempdic[player].pop(0)
        #note: I have to use pop here because in reality I have a list of list but tried simplify the code
        Tempdic[player] = ["differentaction",5]
print(Tempdic)
#{'Bar': ['differentaction', 5], 'Foo': ['Somthing', 1]}
print(Originaldic)
#{'Foo': ['Somthing', 1], 'Bar': [2], 'baz': ['otherthing', 6]}

what I do not understand is why the original dictionary had been modified too. I know you cannot just do dic1 = dic2 but I assumed (wrongly) I was dealing with values of the dictionary here not pointing at the value in a dictionary: 'Bar': [2] instead of "Bar":["action",2] it seem to have done Originaldic["Bar"].pop(0) as well.

EDIT: thank you to Mady for providing an answer, here the issue was not copying two dic like I thought but copying the list that was held in the dictionary (which caused a very similar issue).

  • 2
    This should work `Tempdic[player]=Originaldic[player].copy()`. – Mady Daby Apr 21 '21 at 02:15
  • 3
    Does this answer your question? [How to copy a dictionary and only edit the copy](https://stackoverflow.com/questions/2465921/how-to-copy-a-dictionary-and-only-edit-the-copy) – Mady Daby Apr 21 '21 at 02:17
  • it does, just realized it also applies to list not just dictionaries sorry about the trouble – Nicolas Renaud Apr 21 '21 at 02:17
  • 1
    The surprising thing about Python assignments is that they *never* make a copy of anything. If you need a copy you need to be explicit about it in the right-hand expression. – Mark Ransom Apr 21 '21 at 02:31
  • The issue here was not copying the dictionary but copying the list that was the value held in the dictionary. Which is what got me confused. hopefully it will help someone in the same predicament. – Nicolas Renaud Apr 21 '21 at 02:31

1 Answers1

1

You can copy the value (so you doesn't reference the same value) by using the copy() method:

Originaldic = {"Foo":["Somthing",1], "Bar":["action",2], "baz":["otherthing",6]}
listofplayer = ["Bar","Foo"]
Tempdic = {}
for player in listofplayer:
    Tempdic[player] = Originaldic[player].copy()
    #the problem is likely at this step above

    if Tempdic[player][1]==2:
        Tempdic[player].pop(0)
        #note: I have to use pop here because in reality I have a list of list but tried simplify the code
        Tempdic[player] = ["differentaction",5]

print(Tempdic)
#{'Bar': ['differentaction', 5], 'Foo': ['Somthing', 1]}
print(Originaldic)
#{'Foo': ['Somthing', 1], 'Bar': ['action', 2], 'baz': ['otherthing', 6]}

but you can also use deepcopy to do a deep copy to make sure that non of the dictionary shares any data:

from copy import deepcopy

Originaldic = {"Foo":["Somthing",1], "Bar":["action",2], "baz":["otherthing",6]}
listofplayer = ["Bar","Foo"]
Tempdic = {}
for player in listofplayer:
    Tempdic[player] = deepcopy(Originaldic[player])
    #the problem is likely at this step above

    if Tempdic[player][1]==2:
        Tempdic[player].pop(0)
        #note: I have to use pop here because in reality I have a list of list but tried simplify the code
        Tempdic[player] = ["differentaction",5]

print(Tempdic)
#{'Bar': ['differentaction', 5], 'Foo': ['Somthing', 1]}
print(Originaldic)
#{'Foo': ['Somthing', 1], 'Bar': ['action', 2], 'baz': ['otherthing', 6]}
rorra
  • 9,593
  • 3
  • 39
  • 61