0

For example:

>>> state = (5,[1,2,3])
>>> current_state = state
>>> state[1].remove(3)
>>> state
(5, [1, 2])
>>> current_state
(5, [1, 2])

I changed the state but not current_state. How to keep the current_state value that equals (5,[1,2,3]) instead of removing 3 in python?

thanks!

Stephen
  • 3,822
  • 2
  • 25
  • 45
  • Another option [here](http://stackoverflow.com/questions/15214404/how-can-i-copy-an-immutable-object-like-tuple-in-python). – TigerhawkT3 Aug 31 '15 at 23:26

1 Answers1

1

One option would be to deepcopy state, so it and current_state refer to different objects:

>>> from copy import deepcopy
>>> state = (5,[1,2,3])
>>> current_state = deepcopy(state)
>>> state[1].remove(3)
>>> state
(5, [1, 2])
>>> current_state
(5, [1, 2, 3])
Mureinik
  • 297,002
  • 52
  • 306
  • 350