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

Convert a string of list to a proper list

I have the following:

a = """\"[""123456789"",""987654321""]\""""

I am trying to convert that to a list of strings. I’ve tried the following:

lst = ast.literal_eval(a)

but this returns all the characters as an individual string. What is the mistake I am doing?

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

The expected output: ["123456789", "987654321"]

>Solution :

You can try literal_eval twice to get a list of integers and then map each integer to strings like this :

from ast import literal_eval as le
lst = le(le(a))
lst = list(map(str, lst))

lst would now become ['123456789', '987654321']

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