0

I have string variable in python as

variable = "[['Acc',{'b':1,'s':1}],['ABC',{'c':1,'d':1}]]"

the type of this varible is <class 'str'>

I want output as result = [['Acc',{'b':1,'s':1}],['ABC'],{'c':1,'d':1}] which should be of type <class 'list'>

I dont know how to get this. Plz help me.

1 Answers1

3

ast.literal_eval - Safely evaluate an expression node or a string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, None, bytes and sets.

import ast

variable = "[['Acc',{'b':1,'s':1}],['ABC'],{'c':1,'d':1}]"
list1 = ast.literal_eval(variable)

print(list1)
print(type(list1))

O/P:

[['Acc', {'b': 1, 's': 1}], ['ABC'], {'c': 1, 'd': 1}]
<class 'list'>
bharatk
  • 3,717
  • 5
  • 12
  • 28