Search code examples
pythonpython-3.xlistloopsvariables

Why is a variable automatically modified even though I am not modifying it?


I simplified my code to get the error I required.

one = ['x']
two = ['y']
final_data = []
for r in range(1, 3):
    print(final_data, r)
    if r == 2:
        one[0] = two[0]
        print(one)
    final_data.append(one)
    print(final_data)
print(final_data)

The final data in 2nd loop is not modified but the end result is coming [['y'], ['y']] even though I expect it to come as [['x'], ['y']]


Solution

  • In this line one[0] = two[0] you change the value in list one and in the end when you print final_data you get [['y'], ['y']] because in list one you have 'y'.

    You need for each iteration to create a copy of one. To copy a list you can use [:]. Then after you do what you want with one, copy old_one to one.

    ...
    old_one = one[:]
    ...
    one = old_one[:]
    

    Try this:

    one = ['x']
    two = ['y']
    final_data = []
    for r in range(1, 3):
        print(final_data, r)
        old_one = one[:]
        if r == 2:
            one[0] = two[0]
            print(one)
        final_data.append(one)
        one = old_one[:]
        print(final_data)
    print(final_data)