0
s = u"['1', '2', '2', '1', '2']"
print type(s) # <type 'unicode'>

How can I convert this into list like here:

s = [1,2,2,1,2]
mamasi
  • 915
  • 3
  • 10
  • 17
  • 2
    And how did you end up with such a string in the first place? Perhaps we can suggest better ways to communicate or store a list? – Martijn Pieters Apr 01 '15 at 15:18

1 Answers1

5

You could use ast.literal_eval function.

>>> import ast
>>> s = u"['1', '2', '2', '1', '2']"
>>> list(map(int, ast.literal_eval(s)))
[1, 2, 2, 1, 2]

OR

>>> [int(i) for i in ast.literal_eval(s)]
[1, 2, 2, 1, 2]
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274