Python: why does my list change when I'm not actually changing it?
The reason this is happening can be found here:
mlist = [1,2,3,4,5]
mlist2 = mlist
the second statement "points" mlist2
to mlist
(i.e., they both refer to the same list object) and any changes you make to one is reflected in the other.
To make a copy instead try this (using a slice operation):
mlist = [1,2,3,4,5]
mlist2 = mlist[:]
In case you are curious about slice notation, this SO question Python Lists(Slice method) will give you more background.
Finally, it is not a good idea to use list
as an identifier as Python already uses this identifier for its own data structure (which is the reason I added the "m
" in front of the variable names)
That's because both list
and list2
are referring to the same list after you did the assignment list2=list
.
Try this to see if they are referring to the same objects or different:
id(list)
id(list2)
An example:
>>> list = [1, 2, 3, 4, 5]
>>> list2 = list
>>> id(list)
140496700844944
>>> id(list2)
140496700844944
>>> list.remove(3)
>>> list
[1, 2, 4, 5]
>>> list2
[1, 2, 4, 5]
If you really want to create a duplicate copy of list
such that list2
doesn't refer to the original list but a copy of the list, use the slice operator:
list2 = list[:]
An example:
>>> list
[1, 2, 4, 5]
>>> list2
[1, 2, 4, 5]
>>> list = [1, 2, 3, 4, 5]
>>> list2 = list[:]
>>> id(list)
140496701034792
>>> id(list2)
140496701034864
>>> list.remove(3)
>>> list
[1, 2, 4, 5]
>>> list2
[1, 2, 3, 4, 5]
Also, don't use list
as a variable name, because originally, list
refers to the type list, but by defining your own list
variable, you are hiding the original list
that refers to the type list. Example:
>>> list
<type 'list'>
>>> type(list)
<type 'type'>
>>> list = [1, 2, 3, 4, 5]
>>> list
[1, 2, 3, 4, 5]
>>> type(list)
<type 'list'>