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

How I Convert a string representation of a nested list into a nested list?

I have a string representing a nested list:
string = [[1,2,3],[4,5,6]]

And a would like to convert it to a list:
list = [[1, 2, 3], [4, 5, 6]]

I’ve tried using string.split("]'[").split(', ') but it still returning [ ['1,2,3],[4,5,6'] ]

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

Any help? Thanks!

>Solution :

Assuming that you have a string like string = "[[1,2,3],[4,5,6]]" and you want to convert it to a list object like lst = [[1,2,3],[4,5,6]], you could simply use the eval method:
lst = eval(string)

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