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

alternately flatten values of nested list

Is there a way to flatten values of a nested list but alternately?

example:

nested_list = [[1, 3, 5], [2, 4]]

my expected output would be:

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

[1, 2, 3, 4, 5]

>Solution :

Use hstack from numpy

import numpy as np
nested_list = [[1, 3, 5], [2, 4]]
new_nested_list = np.hstack(nested_list)

now new_nested_list is equal to [1 3 5 2 4]

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