Given a list, I would like to apply some set of operations to a subset(slice) of the list, and store the result of each transformation in the original list.
My background is in Ada, which led me to make the following mistake:
Number_List = [0,1,2,3,4,5,6,7,8,9]
for Index, Number in enumerate(Number_List[1:]):
Number_List[Index] = Number + 1
Giving a new Number_List of: 2,3,4,5,6,7,8,9,10,9 and teaching me that a slice of an array is re-indexed to 0.
I’ve moved to the following, which is cumbersome but functional.
Number_List = [0,1,2,3,4,5,6,7,8,9]
for Index in range(1,len(Number_List))
Number_List[Index] = Number_List[Index]+1
I am looking for a more elegant way to do this.
enumeratetakes astartparameter:You can also write