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)