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

Converting list of arrays into list of lists in Python

I have a list I which contains numpy arrays. I want to convert these arrays into lists as shown in the current and expected outputs.

import numpy as np
I=[np.array([[0, 1],
        [0, 2],
        [1, 3],
        [4, 3],
        [2, 4]]),
 np.array([[0, 1],
        [0, 2],
        [1, 3],
        [4, 3],
        [3, 4],
        [2, 5]])]

for i in range(0,len(I)):
    arI1=[]
    I1=I[i].tolist()
    arI1.append(I1)
    I1=list(arI1)
    print(I1)

The current output is

[[[0, 1], [0, 2], [1, 3], [4, 3], [3, 4], [2, 5]]]

The expected output is

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

[[[0, 1], [0, 2], [1, 3], [4, 3], [2, 4]],
[[0, 1], [0, 2], [1, 3], [4, 3], [3, 4], [2, 5]]]

>Solution :

You can use numpy:

import numpy as np

I1 = np.array(I1, dtype=object).tolist()
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