Python tip 7: creating a deepcopy of collections
From copy built-in module:
Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.
The shared binding is helpful for cases like in-place modification of lists within a user defined function:
>>> def rotate(ip):
... ip.insert(0, ip.pop())
...
>>> nums = [321, 1, 1, 0, 5.3, 2]
>>> rotate(nums)
>>> nums
[2, 321, 1, 1, 0, 5.3]
You can use the copy.deepcopy()
method if you wish to recursively create new copies of all the elements of a mutable object:
>>> import copy
>>> nums_2d = [[1, 3, 2, 10], [1.2, -0.2, 0, 2], [100, 200]]
>>> nums_2d_deepcopy = copy.deepcopy(nums_2d)
>>> nums_2d_deepcopy[0][0] = 'yay'
>>> nums_2d_deepcopy
[['yay', 3, 2, 10], [1.2, -0.2, 0, 2], [100, 200]]
>>> nums_2d
[[1, 3, 2, 10], [1.2, -0.2, 0, 2], [100, 200]]
Video demo:
See Mutability chapter from my 100 Page Python Intro ebook for more details on this topic.