I have a list T1. I want to increase all elements of T1 by 1 except the first element. How do I do so?
T1=[0,1,2,3,4,5,6,7]
T2=[i+1 for i in T1]
The current output is
T2= [1, 2, 3, 4, 5, 6, 7, 8]
The expected output is
T2= [0, 2, 3, 4, 5, 6, 7, 8]
>Solution :
Use slicing!
T1 = [0,1,2,3,4,5,6,7]
T2 = [T1[0]] + [i+1 for i in T1[1:]]
Or, with enumerate:
T1 = [0,1,2,3,4,5,6,7]
T2 = [x+(i>0) for i, x in enumerate(T1)]
Output:
[0, 2, 3, 4, 5, 6, 7, 8]