Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inserting elements within a list in Python

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]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading