Answer by jack for List changes unexpectedly after assignment. Why is this...
There are many answers already that tell you how to make a proper copy, but none of them say why your original 'copy' failed. Python doesn't store values in variables; it binds names to objects. Your...
View ArticleAnswer by Aaron Hall for List changes unexpectedly after assignment. Why is...
What are the options to clone or copy a list in Python?In Python 3, a shallow copy can be made with:a_copy = a_list.copy()In Python 2 and 3, you can get a shallow copy with a full slice of the...
View ArticleAnswer by anatoly techtonik for List changes unexpectedly after assignment....
I've been told that Python 3.3+adds list.copy() method, which should be as fast as slicing:newlist = old_list.copy()
View ArticleAnswer by cryo for List changes unexpectedly after assignment. Why is this...
Felix already provided an excellent answer, but I thought I'd do a speed comparison of the various methods:10.59 sec (105.9us/itn) - copy.deepcopy(old_list)10.16 sec (101.6us/itn) - pure python Copy()...
View ArticleAnswer by Felix Kling for List changes unexpectedly after assignment. Why is...
With new_list = my_list, you don't actually have two lists. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the...
View ArticleAnswer by erisco for List changes unexpectedly after assignment. Why is this...
Python's idiom for doing this is newList = oldList[:]
View ArticleAnswer by Paul Tarjan for List changes unexpectedly after assignment. Why is...
Use thing[:]>>> a = [1,2]>>> b = a[:]>>> a += [3]>>> a[1, 2, 3]>>> b[1, 2]>>>
View ArticleList changes unexpectedly after assignment. Why is this and how to prevent it?
While using new_list = my_list, any modifications to new_list changes my_list every time. Why is this, and how can I clone or copy the list to prevent it?
View ArticleAnswer by fjemi for List changes unexpectedly after assignment. Why is this...
The method to use depends on the contents of the list being copied. If the list contains nested dicts than deepcopy is the only method that works, otherwise most of the methods listed in the answers...
View ArticleAnswer by Laurent Lyaudet for List changes unexpectedly after assignment. Why...
There is another way of copying a list that was not listed until now: adding an empty list: l2 = l + [].I tested it with Python 3.8:l = [1,2,3]l2 = l + []print(l,l2)l[0] = 'a'print(l,l2)It is not the...
View ArticleAnswer by Code Carbonate for List changes unexpectedly after assignment. Why...
There is a simple technique to handle this.Code:number=[1,2,3,4,5,6] #Original listanother=[] #another empty listfor a in number: #here I am declaring variable (a) as an item in the list (number)...
View ArticleAnswer by Roshin Raphel for List changes unexpectedly after assignment. Why...
This is because, the line new_list = my_list assigns a new reference to the variable my_list which is new_listThis is similar to the C code given below,int my_list[] = [1,2,3,4];int *new_list;new_list...
View ArticleAnswer by shahar_m for List changes unexpectedly after assignment. Why is...
The deepcopy option is the only method that works for me:from copy import deepcopya = [ [ list(range(1, 3)) for i in range(3) ] ]b =...
View ArticleAnswer by Dr. Hippo for List changes unexpectedly after assignment. Why is...
Remember that in Python when you do: list1 = ['apples','bananas','pineapples'] list2 = list1List2 isn't storing the actual list, but a reference to list1. So when you do anything to list1, list2...
View ArticleAnswer by B.Mr.W. for List changes unexpectedly after assignment. Why is this...
A slight practical perspective to look into memory through id and gc. >>> b = a = ['hell', 'word']>>> c = ['hell', 'word']>>> id(a), id(b), id(c)(4424020872, 4424020872,...
View ArticleAnswer by Corman for List changes unexpectedly after assignment. Why is this...
I wanted to post something a bit different than some of the other answers. Even though this is most likely not the most understandable, or fastest option, it provides a bit of an inside view of how...
View ArticleAnswer by Chris_Rands for List changes unexpectedly after assignment. Why is...
Note that there are some cases where if you have defined your own custom class and you want to keep the attributes then you should use copy.copy() or copy.deepcopy() rather than the alternatives, for...
View ArticleAnswer by SCB for List changes unexpectedly after assignment. Why is this and...
It surprises me that this hasn't been mentioned yet, so for the sake of completeness...You can perform list unpacking with the "splat operator": *, which will also copy elements of your list.old_list =...
View ArticleAnswer by Aaditya Ura for List changes unexpectedly after assignment. Why is...
Let's start from the beginning and explore this question.So let's suppose you have two lists:list_1 = ['01', '98']list_2 = [['01', '98']]And we have to copy both lists, now starting from the first...
View ArticleAnswer by jainashish for List changes unexpectedly after assignment. Why is...
A very simple approach independent of python version was missing in already-given answers which you can use most of the time (at least I do):new_list = my_list * 1 # Solution 1 when you are not using...
View Article