1

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?

user2196600
  • 89
  • 1
  • 5
  • See this question: http://stackoverflow.com/questions/838642/python-dictionary-deepcopy – Neil Mar 21 '13 at 20:23

1 Answers1

0

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')