This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 6 years ago.
I have a string, something like:
a = "[1, 2, 3]"
If there an easy way to convert it in to a list, without using .split(), join() etc.
Thanks for any replies.
use ast.literal_eval() it's safer than using eval
from ast import literal_eval
a = literal_eval("[1, 2, 3]")
print(a)
Related
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 5 months ago.
I have message which is a list and JSON in it. I managed to get JSON with raw decode. The problem is
string = "["a,b","b,c"]"
How can I convert that string into a list?
Use ast.literal_eval:
import ast
print(ast.literal_eval(string))
Since this is JSON, use the json module:
import json
print(json.loads(string))
string = "['a,b','b,c']"
print(eval(string))
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 11 months ago.
Say the string is like a = "['Hello', 'World']". After the conversion, a = ['Hello', 'World'] and the type is a list.
It's called expression evaluation. Read about Python's eval() and literal_eval().
Notice that it may be dangerous, so read the docs carefully.
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 3 years ago.
I have a form that save lists as strings, I want to convert those strings to lists before save in DB.
I'm using python3 and Django.
The strings looks like:
'["MEX","MTY","GDL","BJX","QRO","PBC","TLC","CUN","EM","CUU"]'
what will be the most pythonic way to do that?
import json
json.loads('["MEX","MTY","GDL","BJX","QRO","PBC","TLC","CUN","EM","CUU"]')
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 6 years ago.
It's really odd but I have this string:
"['please', 'help']"
I want something that would get one argument at a time.
I've searched everywhere for this but I didn't find anything.
Thanks in advance
While eval is a correct approach, it can often have negative consequences. I suggest using ast.literal_eval, which is a more safe approach (as mentioned by the linked docs):
import ast
s = "['please', 'help']"
s_list = ast.literal_eval(s)
print s_list
Are you looking for something like this?
string = "['please', 'help']"
string_list = eval(string)
print string_list[0], string_list[1]
Edit: you should ideally use ast.literal_eval as the other answer suggests, if you are unsure of what the string contains.
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
How do I reverse each word in a string, but in the same sentence order? [duplicate]
(1 answer)
Closed 6 years ago.
How to reverse string in python? my input string is "Hey Gun" now i want to display "Gun Hey" as output. I have tried using slice operator like [::-1] but it won't shows proper output how it works in python?
Do splitting and then reversing and then joining.
' '.join(string.split()[::-1])
Using split and reversed (a bit slower though):
>>> a
'Hey Gun'
>>> ' '.join(reversed(a.split()))
'Gun Hey'