-2

I have a string a = '["Internet Software","IoT"]'. It is not a list. It is a string of length 27.

I want to convert this into a list containing elements : Internet Software and IoT.

How do I do so?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555

2 Answers2

0

use eval()

a = '["Internet Software","IoT"]'
lst = eval(a)
print type(lst)
# <type 'list'>
Ghilas BELHADJ
  • 13,412
  • 10
  • 59
  • 99
0

You have at least 2 options: eval and json.loads

import json

a = '["Internet Software","IoT"]'
print(eval(a)) # ['Internet Software', 'IoT']
print(json.loads(a)) # ['Internet Software', 'IoT']

I would recommend against eval, because it could execute malicious code.

json.loads on the other hand only works if your string is valid JSON (which is true for the string you posted).

Mike Scotty
  • 10,530
  • 5
  • 38
  • 50