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>
matebende
  • 543
  • 1
  • 7
  • 21
Anoop
  • 505
  • 8
  • 23

1 Answers1

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']
user2390182
  • 72,016
  • 6
  • 67
  • 89