Don't forget you're performing a shallow copy though, if the list contains an object, you're sharing the same object between the original list and the copy:
as you can see now both lists, the original and the copy, have been modified, this is because the list contain a share object (the list ls1). If you want a deep copy you can use copy.deepcopy():
Hi @cuongld2 !
Another way is to use the unpacking operator:
Don't forget you're performing a shallow copy though, if the list contains an object, you're sharing the same object between the original list and the copy:
as you can see now both lists, the original and the copy, have been modified, this is because the list contain a share object (the list
ls1
). If you want a deep copy you can use copy.deepcopy():There are limitations to what it can "deepcopy", the documentation contains further details.
Please don't use list unpacking for the sole purpose of creating a shallow copy of a list. Lists have a
.copy
method that should be used.I agree, my fault :) I was enumerating the various options :)