In your first case the elements of the subsets list not change because you make a new list of subsets(Both names are same) and add the values in this list.
But In your second case you update your list values while iterating through it, THis is why you got different output.]
Execution of your second code.
subsets = [0]*15
subsets[0] = 2
for index,value in enumerate(subsets):
subsets[index] = index + subsets[subsets[index]
print(subsets) ## [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
- In the first iteration
(index=0), index + subsets[subsets[index]]
(subsets[index]=2) and after subsets[index]=2 (subsets[2]=0) result is now equal to 0 + 0 which is 0.
- In the second iteration
(index=1), index + subsets[subsets[index]]
(subsets[index]=0) and after subsets[0]=0 (because you change the zero'th value to 0 in first iteration) (subsets[2]=0) result is now equal to 1 + 0 which is 1.
similarly your sum of index + subsets[subsets[index]] is always equal to index value of the iteration.
I hope you understand