Python beginner question:
Is there a way to convert a string of the form '[a, b, c]'
directly into a list?
As in type(f('[a, b, c]')) is list == True
/isinstance(f('[a, b, c]'), list) is True
for some method or function f
? Please note, the final lists should have the elements as individual strings, i.e. f('[a, b, c]') returns ['a', 'b', 'c']
I have checked this and this. ast.literal_eval()
Fails with error:
ValueError: malformed string
My input list (string) isn't unicode marked, and the items are not individually string
or int
.
Is there a builtin for this kind of conversion? This is essentially a 'form' question. I am trying to understand if there is a pythonic
way of doing this without writing a longish function using splitting the string and conversion.
Insights would be much appreciated.