i got a listx =[1,2,3,4,5,6,7,8,9]
i want to alter every Nth item of the list. For example i want to modify for every 2 item step, let say i want to modify by +1 .
so i want to get result = [1+1,2,3+1,4,5+1,6,7+1,8,9+1] =[2,2,4,4,6,6,8,8,9]
i can do this by using for-loop , by adding counter variable , then check the counter by counter%2==0. But this time i 'm just curious using single line statement. Here what i want =
newlistx=[i+1 for i in listx] <- this will modify all items, so i'm expecting i can use some internal indexing use in this iteration process, become like this :
newlistx=[i+1 if (__indexing__%step==0) else i for i in listx] where step=2.
Actually, i can use list.index() function , like this :
newlistx=[i+1 if listx.index(i)%2==0 else i for i in listx]
the problem this me thod only works if all the item is unique, if i got items which have same value then index() will return wrong value.
Again, i'm just curious if i can grab some internal indexing or counter , if exist.