Edit the value of every Nth item in a list

Viewed 4761

What's the most pythonic way of performing an arithmetic operation on every nth value in a list? For example, if I start with list1:

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

I would like to add 1 to every second item, which would give:

list2 = [1, 3, 3, 5, 5, 7, 7, 9, 9, 11]

I've tried:

list1[::2]+1

and also:

for x in list1:
    x=2        
    list2 = list1[::x] + 1
7 Answers
Related