-2

Quick question, some data was corrupted and now all the lists I had have turned into strings:

e.g: ["en", "ru"],

is now :

str(["en", "ru"])

What is the best way to turn it back to list?

Taufiq Rahman
  • 5,436
  • 2
  • 35
  • 43
Ivan Bilan
  • 2,219
  • 4
  • 32
  • 54
  • Found a duplicate: http://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list-in-python Thanks everyone! – Ivan Bilan Dec 09 '16 at 09:07

2 Answers2

4

Use ast.literal_eval. Do not use eval it is unsafe.

>>> import ast
>>> ast.literal_eval(str(["en", "ru"]))
['en', 'ru']
Chris_Rands
  • 35,097
  • 12
  • 75
  • 106
1

eval(str(["a", "b"]))

gets you ["a", "b"]

jf328
  • 5,975
  • 8
  • 49
  • 78