I just start to learn coding in Python. Please allow me to ask a stupid question which has bothered me for a long time.
g={1:'a',2:'b',3:["k",'c'],4:'d'}
gt=g
gt[3].remove('c')
How can I leave the g unchanged?
I just start to learn coding in Python. Please allow me to ask a stupid question which has bothered me for a long time.
g={1:'a',2:'b',3:["k",'c'],4:'d'}
gt=g
gt[3].remove('c')
How can I leave the g unchanged?
Use copy for this. And since your copying a dictionary that contains a list and you want to edit that list, you should use deepcopy:
from copy import deepcopy
g={1:'a',2:'b',3:["k",'c'],4:'d'}
gt=copy.deepcopy(g)
gt[3].remove('c')