Inserting elements within a list in Python

Advertisements

I have a list A with first and last elements 20 and 0 respectively. I am inserting elements in A through C but I want to do so in one step and without writing everything manually.

len_A=6
A=[20,0]
B=A[0]/(len_A-1)
C=[A[0],A[0]-B, A[0]-2*B,A[0]-3*B,A[0]-4*B,0]
print(C)

The current and expected output is

[20, 16.0, 12.0, 8.0, 4.0, 0]

>Solution :

You can try the below:

len_A = 6
A = [20, 0]
B = A[0] / (len_A - 1)
C = [A[0] - i * B for i in range(len_A)]
print(C)

Output:

[20, 16.0, 12.0, 8.0, 4.0, 0]

Leave a ReplyCancel reply