0

I have 2 lists(arrays) that I mingle with. What I want is copy the the first list into second list. After that in a loop change the second list elements with '-'. Result is both lists and their all elements filled with '-'. Normally I expected the first list like "Y1,Y2,Y3" etc and the second list "-,-,-". It's so annoying even when I put a debug point in loop they are both changing one by one into '-'. Problem is why for loop ignores first line and executes second line and somehow alters first list. This is one of blocks from the code and there are multiple examples like these. Now I'm worrying about all of them is problematic or not.

##
s1     = ['1','2','3','4',\
                '5','6','7','8',\
                '9','10']             


s2 = s1
for m in range(len(s1)):
        s1[m] = 'Y' + s1[m]
        s2[m] = '-'
print s1
print s2
  • 4
    `s2 = s1` doesn't copy `s1`, it makes `s2` refer to the same list as `s1`. Hence, when you change either you change both. – asongtoruin Feb 06 '17 at 13:10
  • `s2 = s1[:]` will create a shallow copy. Otherwise you are just assigning a reference to the same list, hence why both change, since it is just one list. – Steven Summers Feb 06 '17 at 13:10

0 Answers0