copy module in python code example
Example 1: example of a deep copy in python
# deep copy example in python
import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.deepcopy(old_list)
old_list[1][0] = 'BB'
print("Old list:", old_list)
print("New list:", new_list)
# OUTPUT
Old list: [[1, 1, 1], ['BB', 2, 2], [3, 3, 3]]
New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
Example 2: python clone object
import copy
new_ob = copy.deepcopy(old_ob)
Example 3: .copy python
new_list = list.copy()
# returns a new list without modifying the orginal list.