0

I need to convert a string of list to List in Python. I have seen many of the similar questions but none of them works in this case.

I am passing some values through PostMan.

The key passing as a form data

Key =  controls
value = [CR1,CR2]

I am fetching the data like this

c_list = self._kwargs['data'].get('controls', [])
print(c-list)
print(type(c-list))

I am getting the following o/p

[CC-2,CC-3]
<class 'str'>

But I need to get it as a list so I have tried the following method

import ast
c_list = self._kwargs['data'].get('controls', [])
res = ast.literal_eval(c_list)

But I am getting the following Error

malformed node or string: <_ast.Name object at 0x7f82966942b0>
2

1 Answer 1

3

You could simply do the following: strip the brackets and split on the commas

>>> s = "[CC-2,CC-3]"
>>> s.strip('[]').split(',')
['CC-2', 'CC-3']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.