my_list = [1, 2]
print my_list[0]
1
x = my_list[0]
x += 2
now 'x' equals 3 and my_list[0] also equals 3. How can I preserve my list elements so they remain unchanged?
my_list = [1, 2]
print my_list[0]
1
x = my_list[0]
x += 2
now 'x' equals 3 and my_list[0] also equals 3. How can I preserve my list elements so they remain unchanged?
You can slice it:
new_list = old_list[:]
or
import copy
new_list = copy.copy(old_list)
If this helped please voted up & marked “accepted”