Numpy arrays can handle such slice operations:
import numpy as np
List = np.array([1, 2, 3, 4, 5, 6])
List[2:5] += 5
print(List)
Numpy will really come in handy for you if you have many of such tasks to do in your code. However, if it's just a one time thing in your code, you can do:
List = [1, 2, 3, 4, 5, 6]
for i in range(2, 5):
List[i] += 5
print(List)
Output:
[1, 2, 8, 9, 10, 6]
EDIT
Addressing your edit, you can also use numpy arrays like so:
import numpy as np
List = np.array([[1, 2, 3], [4, 5, 6]])
List[0] += 5
print(List)
Or using a loop:
List = [[1, 2, 3], [4, 5, 6]]
for i in range(len(List[0])):
List[0][i] += 5
print(List)
Output:
[[6, 7, 8], [4, 5, 6]]
my_list = [n + 5 if i in range(2, 5) else n for i, n in enumerate(my_list)]range(2, 4):)rangeworks. :) OP wants to add to every item in indices 2 to 4 inclusive.