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

Repeat a list's elements in the same order

I have a list, and I want to repeat its elements in the same order.

I want to create bg using sl.

# small list
sl = [10,20]
# from small list, create a big list
bg = [10,10,20,20]

I tried:

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

bg = [[i,j] in for i,j in zip([[sl[0]]*2,[sl[1]]*2])]

But this gives me:

 bg = [[10,20],[10,20]]

How can I get the desired output?

>Solution :

import numpy as np
s1 = [10,20]
s2 = np.array(s1).repeat(2)
print(list(s2)) # [10, 10, 20, 20]

I could not resist the urge to use numpy in such operations. With this function you can repeat not only for single dimensional cases but also in case of matrix or higher order tensors as well.

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