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

Repeating a certain line several times based on the length of a list in Python

I want to repeat the line A=A[:1] + [i + 1 for i in A] several times based on the length of B. For instance, len(B)=4 and A=A[:1] + [i + 1 for i in A] has been repeated 4 times. Is there a way to repeat A=A[:1] + [i + 1 for i in A] without writing it explicitly and get the same current output?

A=[0,1,2,3,4,5,6,7]  
B=[1,3,6,7]          

A=A[:1] + [i + 1 for i in A]
A=A[:1] + [i + 1 for i in A]
A=A[:1] + [i + 1 for i in A]
A=A[:1] + [i + 1 for i in A]
print(A)

The current output is

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

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 :

Try this:

for j in range(len(B)):
A = A[:1] + [i + 1 for i in A]

Then just print normally after the for loop.

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