Even if a part of the code is missing, I suppose you're facing the following problem:
dear_old_value = ["rusty_value0", "rusty_value1", "rusty_value2", "rusty_value3"]
new_values = dear_old_value
new_values[0] = "shiny_value0"
new_values[2] = "shiny_value2"
print dear_old_value # OUTPUT: ['shiny_value0', 'rusty_value1', 'shiny_value2', 'rusty_value3']
as you see, even if you're modifying values in the new list, also the old one gets modified.
To work around this problem, you should assign to the new list a copy of the old one:
dear_old_value = ["rusty_value0", "rusty_value1", "rusty_value2", "rusty_value3"]
new_values = list(dear_old_value)
new_values[0] = "shiny_value0"
new_values[2] = "shiny_value2"
print dear_old_value # OUTPUT: ['rusty_value0', 'rusty_value1', 'rusty_value2', 'rusty_value3']
In Python, you should think at variable names as tags for objects.
In our case we are not assigning the values contained in dear_old_values
to new_values
, we are simply saying that the object referenced by dear_old_values
will now be also referenced by new_values
.
You can check the ID of your object to confirm this:
dear_old_value = ["rusty_value0", "rusty_value1", "rusty_value2", "rusty_value3"]
new_values = dear_old_value
print id(dear_old_value) # 50779920
print id(new_values) # 50779920
To avoid this, we initialize our variable (new_values
) as a new list
object (with a different position in memory), then we push the items in dear_old_varables
into it.
If you check now the IDs you'll get this result:
dear_old_value = ["rusty_value0", "rusty_value1", "rusty_value2", "rusty_value3"]
new_values = list(dear_old_value)
print id(dear_old_value) # 41342736
print id(new_values) # 41358240
Keep in mind that all the objects in Python behaves like this.