I have this code:
def change_element(list, index, new_element):
list[index] = new_element
return list
example_list = [1, 2, 3]
new_list = change_element(example_list, -1, 4)
print example_list
print new_list
I don't want the variable "example_list" to be changed. I believe that the function "change_element" argument "list" is a pointer to "example_list", so when "list" is changed, "example_list" is changed. Even with this (below), "example_list" is still changed.
def change_element(list, index, new_element):
return_list = list
return_list[index] = new_element
return return_list
example_list = [1, 2, 3]
new_list = change_element(example_list, -1, 4)
print example_list
print new_list
How do I prevent this from happening? I know this is possible with loops, but the lists I want to iterate are large. (maybe greater than 10,000 elements)