Search code examples
pythonpython-3.xcastingabstract-data-type

Python Casting Nested String of Lists to List


Does anyone know a simple way to convert the given list into the desired format?

Given:

['[[2.1, 3.7], [5.5, -1.3]]']

Desired Format:

[[2.1, 3.7], [5.5, -1.3]]

Thanks in advance


Solution

  • Try this:

    import ast
    lst = ['[[2.1, 3.7], [5.5, -1.3]]']
    ast.literal_eval(lst[0])
    => [[2.1, 3.7], [5.5, -1.3]]