0

I'm trying to build a new key into my dictionary by taking another one from the same dictionary. And I would like to change some values in that new key.

Let's look at an example:

# Here is a dictionary
my_dict = {'one' : {'same': 'test_1'}}

# Create a new key based on a pre-existing one in our dictionary
my_dict['two'] = my_dict['one']

# Asign a new value to that new key:
my_dict['two']['same'] = 'test_2'

What I expected when print(my_dict):

{'one': {'same': 'test_1'}, 'two': {'same': 'test_2'}}

But here is what I got:

{'one': {'same': 'test_2'}, 'two': {'same': 'test_2'}}

My question is: why both one and two have changed? Why not just two like I asked?

Also if we try:

# Creating a dictionary but this time, fully prepared
my_dict = {'one': {'same': 'test_1'}, 'two': {'same': 'test_1'}}

# Changing a value in key two:
my_dict['two']['same'] = 'test_2'

We get print(my_dict):

{'one': {'same': 'test_1'}, 'two': {'same': 'test_2'}}

Which this time, is the output I expected.

What's the difference between this two methods?

Edit Answer:

It was not working because at my_dict['two'] = my_dict['one'] it was assigning the same reference to both keys. So if one of them is changed, all the corresponding references will change too.

To avoid this as suggested in the answers: my_dict['two'] = my_dict['one'].copy() will copy the key but without the reference.

More info in another post: https://stackoverflow.com/a/2465932/10972294

This post is a duplicated, but as suggested here: https://meta.stackoverflow.com/a/265737/10972294

This post still might help for those who are not aware of the copy & reference stuff in python dictionaries.

RobZ
  • 496
  • 1
  • 10
  • 26
  • 1
    Simple, when you perform `my_dict['two'] = my_dict['one']` then both point to the same instance by reference. So as both point to the same instance, changing that one instance through `two` will be reflected in `one`. – Maarten Bodewes Jan 07 '21 at 22:17
  • 1
    You need to make a copy of the dictionary by using the its `copy()` method. – martineau Jan 07 '21 at 22:17
  • 1
    It is because if this `my_dict['two'] = my_dict['one']` this creates the reference between the objects as you have assigned that value from `one` and when you change `two`, the other gets changed – jedimasterbot Jan 07 '21 at 22:18

3 Answers3

1

That should do the trick in order to avoid reference issues.

my_dict['two'] = my_dict['one'].copy()
WArnold
  • 275
  • 2
  • 10
0

At the time of assignment using my_dict['two'] = my_dict['one'], you assigned the reference to the object which my_dict['one'] was pointing to my_dict['two'].

Now you have two keys in the dict pointing to same object.

>>> my_dict = {'one' : {'same': 'test_1'}}
>>> my_dict['two'] = my_dict['one']

# You didn't just created a key, but also stored the same referrence
>>> my_dict['two']
{'same': 'test_1'}

# object pointed by my_dict['two'] is as same my_dict['one']
>>> my_dict['two'] is my_dict['one']
True

Hence any update in the object of any of the key will reflect for both the keys as they are referring same object.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

Python basically uses references for lists and dict. You need to unpack it using **

>>> my_dict = {'one' : {'same': 'test_1'}}
>>> my_dict['two'] = {**my_dict['one']}
>>> my_dict
{'one': {'same': 'test_1'}, 'two': {'same': 'test_1'}}
>>> my_dict["two"]["same"] = "test_2"
>>> my_dict
{'one': {'same': 'test_1'}, 'two': {'same': 'test_2'}}
tbhaxor
  • 1,659
  • 2
  • 13
  • 43