Python dictionaries copy function

Viewed 34
dict_c = {1: {2: 3}, 4: 5}

dict_d = dict_c.copy()

dict_d[4] = 9

dict_d[1][2] = 9

print(dict_c)

Answer:

{1: {2: 9}, 4: 5}

Why is the output not {1: {2: 9}, 4: 9} instead? I just started coding and one of the practice I came across this question, thank you so much for the help

1 Answers

It is your printing the wrong one

In [106]: dict_c = {1: {2: 3}, 4: 5}
     ...:
     ...: dict_d = dict_c.copy()
     ...:
     ...: dict_d[4] = 9
     ...:
     ...: dict_d[1][2] = 9
     ...:
     ...: print(dict_c)
     ...: print(dict_d)
{1: {2: 9}, 4: 5}
{1: {2: 9}, 4: 9}
Related