Convert String into list in python [duplicate] - python

This question already has answers here:
Converting JSON objects in to dictionary in python
(3 answers)
Closed 5 years ago.
I am trying to convert the following string into list. However I get the variable enclosed in round brackets.
a = '{"0": "407-1656"}, {"4": "512-873"}'
b = [a]
on executing above following is the value of b
['{"0": "407-1656"}, {"4": "512-873"}']
I require the output to be
[{"0": "407-1656"}, {"4": "512-873"}]
Thanks.

You can use ast.literal_eval()
>>> from ast import literal_eval
>>> b = literal_eval(a)
>>> b
({'0': '407-1656'}, {'4': '512-873'})

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'>

How to convert a list stored as a string to a list? [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Converting a string representation of a list into an actual list object [duplicate]
(3 answers)
Closed 3 years ago.
I know the question sounds a bit vague, but this should clear it up.
a = "[200, 30.5, 37]" #This is a string literal, where a[0] = '['
b = [200, 30.5, 37] #This is a list literal, where b[0] = 200
How do I get b from a?
b = eval(a)
This should do the trick.

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)]

Python: string to list [duplicate]

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']

Best way to get the list-like string back into a list [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 6 years ago.
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?
Use ast.literal_eval. Do not use eval it is unsafe.
>>> import ast
>>> ast.literal_eval(str(["en", "ru"]))
['en', 'ru']
eval(str(["a", "b"]))
gets you ["a", "b"]

Categories