+ 11
How do I make a copy of a list and work on it separately.
For instance if I say my_list = new_list, any adjustment to new_list affects my_list. Is there a way to copy a list and work on it separately.
3 Respostas
+ 10
You can import copy module and use deepcopy
i.e
import copy
alist = [1,2,3,4,[5,6]]
newlst = copy.deepcopy(alist)
+ 5
new_list = old_list[:]
+ 4
Use copy or [:] if you are absolutely sure that your list won't have lists or other mutable in it. Otherwise use deepcopy.