+ 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.

11th Sep 2019, 10:36 PM
Meny Evolving
Meny Evolving - avatar
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)
17th Sep 2019, 8:28 PM
BlackRose Mike
BlackRose Mike - avatar
+ 5
new_list = old_list[:]
11th Sep 2019, 10:41 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
Use copy or [:] if you are absolutely sure that your list won't have lists or other mutable in it. Otherwise use deepcopy.
12th Sep 2019, 4:18 AM
Thoq!
Thoq! - avatar