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

Combing lists into list in Python

I have these lists in Python

text_0 = ['Weight','Weight', 'Weight']
text_1 = [['x','y','z'],['x','y','z'],['x','y','z']]
text_2 = [['1','2,','3'],['4','5','6'],['7','8','9']]

I would like would to create a new list like this

new_list = ['Weight','x','1','y','2','z','3','Weight','x','4','y','5','z','6','Weight','x','7','y','8','z','9']

How do I do this in Python?

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 :

In your case we can do with python

sum([[x] + [ items for item in zip(y,z) for items in item] 
       for x, y, z in zip(text_0,text_1,text_2)],[])
['Weight', 'x', '1', 'y', '2', 'z', '3', 'Weight', 'x', '4', 'y', '5', 'z', '6', 'Weight', 'x', '7', 'y', '8', 'z', '9']
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