Here is my code:
for each in range(0, number_of_trials):
temp_list = source_list
for i in range(10):
x = random.randrange(0, len(temp_list))
board[i] = temp_list[x]
del temp_list[x]
This code is deleting each element from temp_list, as would be expected. But temp_list is not being reset each time the initial for loop runs, setting it back to source_list. As a result, every delete from temp_list is permanent, lasting for every following iteration of the for loop. How can I avoid this and have temp_list “reset” back to its initial status each time?
The statement
temp_list = source_listdoes not create a new list. It gives a new nametemp_listto an existing list. It doesn’t matter what name you use to access the list—any changes made via one name will be visible via another.Instead, you need to copy the list, like this:
This creates a new list that starts with the same contents as
source_list. Now you can change the new list without affecting the original.