python clone object
import copy
new_ob = copy.deepcopy(old_ob)
python clone object
import copy
new_ob = copy.deepcopy(old_ob)
python deepcopy
>>> import copy
>>> nums = [1, 2, 3]
>>> data = {'a': 10, 'b': nums}
>>> data
{'a': 10, 'b': [1, 2, 3]}
>>> data_copy = copy.copy(data)
>>> data_deep = copy.deepcopy(data)
>>> data_copy
{'a': 10, 'b': [1, 2, 3]}
>>> data_deep
{'a': 10, 'b': [1, 2, 3]}
>>> data_copy['a'] += 2
>>> nums[1:1] = [254]
>>> data
{'a': 10, 'b': [1, 254, 2, 3]}
>>> data_copy
{'a': 12, 'b': [1, 254, 2, 3]}
>>> data_deep
{'a': 10, 'b': [1, 2, 3]}
.copy python
new_list = list.copy()
# returns a new list without modifying the orginal list.
Python Deep Copy
import copy
l1 = [1,3,[9,4],6,8]
l2 = copy.copy(l1) #Making a shallow copy
print('List 1 = ', l1)
print('List 2 = ', l2)
print('Performing change in list 2')
l2[2][0] = 5
print('List 1 = ',l1)
print('List 2 = ',l2)
example of deep copy in python
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 'a']]
new_list = old_list
new_list[2][2] = 9
print('Old List:', old_list)
print('ID of Old List:', id(old_list))
print('New List:', new_list)
print('ID of New List:', id(new_list))
Python Deep Copy
import copy
l1=[1,3,[9,4],6,8]
l2=copy.deepcopy(l1) #Making a deep copy
print('List 1 = ', l1)
print('List 2 = ', l2)
print('Performing change in list 2')
l2[2][0] = 5
print('List 1 = ',l1)
print('List 2 = ',l2)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us