python - How to make a copy of a list of objects not change when modifying the original list? -
python - How to make a copy of a list of objects not change when modifying the original list? -
possible duplicates: how clone list in python? what best way re-create list in python?
original_list = [object1(), object2(), object3()] copy_list = original_list original_list.pop()
if remove object original list, how can maintain re-create list changing well?
original list
[<object.object instance @ 0x00ea29e0>, <object.object instance @ 0x00ea2dc8>, <object.object instance @ 0x00ea2ee0>]
copy list after popping original list (i want equal above)
[<object.object instance @ 0x00ea29e0>, <object.object instance @ 0x00ea2dc8>]
use copy.deepcopy() deep copy:
import re-create copy_list = copy.deepcopy(original_list)
for shallow re-create utilize copy.copy():
import re-create copy_list = copy.copy(original_list)
or piece no endpoints specified :
copy_list = original_list[:]
see copy docs explanation deep & shallow copies.
python list object
Comments
Post a Comment