I was wondering how I could change a list written as a string, such as ‘[1,[2,[]],[3,[]]]’ into an actual python list like [1,[2,[]],[3,[]]]. Is there any algorithm for this?
Asked
Active
Viewed 94 times
1
-
What about `json.loads` ? – Dec 04 '17 at 19:29
-
6Possible duplicate of [Convert string representation of list to list in Python](https://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list-in-python) – B. M. Dec 04 '17 at 19:29
-
3`eval("[1,[2,[]],[3,[]]]")` – Woody Pride Dec 04 '17 at 19:35
-
@WoodyPride `eval` is dangerous because it will execute code in the input. – Skippy le Grand Gourou Jun 19 '19 at 09:27
2 Answers
-1
This topic have a lot of examples of solutions for your problem :)
Python - convert string to list
Regards,

Ricardo Costardi
- 19
- 5
-2
Assign your value to a variable then call the variable.split() it defaults to comma delimited which in your instance would result in list['[1,[2,[]],[3,[]]]']
another example of this could be this:
a = "test, 1, testing2, alpha, beta, Zulu, 23"
a.split()
Result: ['test,', '1,', 'testing2,', 'alpha,', 'beta,', 'Zulu,', '23']

Mike Pineau
- 31
- 4
-
sorry I forgot that split is default space delimited if you do a.split(",") you end up with a comma delimited python list – Mike Pineau Dec 04 '17 at 19:34