Python: string to list [duplicate] - python

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 5 years ago.
how to convert a sting of the format "[u'logic', u'jackpot']" into a list of the format ['logic', 'jackpot']?
Note: "".split() didn't work. It gave me: ["[u'logic',",
"u'jackpot',", "u'420',", "u'pvp", "label',", "u'cassius", "cake',", "u'pod",
"vlivem',", "u'EP',", "u'yzo',", "u'hank", "moody']"]

You can use the safe literal_eval from ast like so:
from ast import literal_eval as leval
print(leval("[u'logic', u'jackpot']")) # -> ['logic', 'jackpot']

Related

Transform string to list of strings [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 1 year ago.
I have this string
'['foo', 'faa', 'fee']'
I want to transform it as a list of strings like:
['foo', 'faa', 'fee']
how could I do that?
Use ast package
from ast import literal_eval
s = "['foo', 'faa', 'fee']"
l = literal_eval(s)
print(type(l))
Output
<class 'list'>

Python doesn't convert string list into regular list? [duplicate]

This question already has answers here:
Convert a String representation of a Dictionary to a dictionary
(11 answers)
How to convert string representation of list to a list
(19 answers)
Closed 3 years ago.
I have a string ouput which looks like below
result = "[(u'Delhi', 20199330), (u'Mumbai', 134869470), (u'Kolkata', 6678446)]"
Now I want to convert it to a regular array. So this is what I do
import json
print(json.loads(result))
But I get the following error
ValueError: No JSON object could be decoded
Now I know result is not a json. But python does convert a string list into a regular list by doing something like
some_list = "[10, 20, 30]"
print(json.loads(some_list))
So I was hoping it would convert my result which is a string list of tuples into a regular list of tuples. But it throws error.
How can I convert result into a regular list?
Try this,
>>> result = "[(u'Delhi', 20199330), (u'Mumbai', 134869470), (u'Kolkata', 6678446)]"
>>> import ast
>>> ast.literal_eval(result)
[('Delhi', 20199330), ('Mumbai', 134869470), ('Kolkata', 6678446)]

How to convert a string of a list into a list in Python? [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 4 years ago.
I have a text file filled with lines like this:
[[19.305199999999999, -126.56959999999999], [19.445499999999999, -126.46599999999999], [19.196999999999999, -126.396], [19.130700000000001, -126.65900000000001], [19.378900000000002, -126.73]]
When I read these lines, they are interpreted as strings. Is there any way I can read them as lists or arrays or easily convert them into lists or arrays?
Use the ast module
Ex:
import ast
l = "[[19.305199999999999, -126.56959999999999], [19.445499999999999, -126.46599999999999], [19.196999999999999, -126.396], [19.130700000000001, -126.65900000000001], [19.378900000000002, -126.73]]"
print(ast.literal_eval(l))
Output:
[[19.3052, -126.5696], [19.4455, -126.466], [19.197, -126.396], [19.1307, -126.659], [19.3789, -126.73]]

turning a list of list of 1 string into a list of tuples [duplicate]

This question already has answers here:
Convert a String representation of a Dictionary to a dictionary
(11 answers)
Closed 4 years ago.
I have a list of one string that looks like this:
['(0.027725, 0.0088202301), (0.00055000000000000003, 0.0040760101),
(0.1666, 0.0020067799), (0.00545, 0.021263899)']
But I want it to be a list of tuples that look that this:
[(0.027725, 0.0088202301),
(0.00055000000000000003, 0.0040760101),
(0.1666, 0.0020067799),
(0.00545, 0.021263899)]
Does anyone know how to do this?
You can use ast.literal_eval:
import ast
s = ['(0.027725, 0.0088202301), (0.00055000000000000003, 0.0040760101), (0.1666, 0.0020067799), (0.00545, 0.021263899)']
new_s = ast.literal_eval('[{}]'.format(s[0]))
Output:
[(0.027725, 0.0088202301), (0.00055, 0.0040760101), (0.1666, 0.0020067799), (0.00545, 0.021263899)]

Convert python list to string [duplicate]

This question already has answers here:
Converting a list to a string [duplicate]
(8 answers)
Closed 9 years ago.
I have a python list like so:
list = []
for loop
list.append("blah")
What I want to do it convert this list to string with each value separated by comma.
How do I do this??
Use the str.join method:
','.join(your_list)

Categories