I have small nested list like this ["['95', '66', '137', '70', '20']"]. I want to remove " for my nested list. How to do this?
I have small nested list like this ["['95', '66', '137', '70', '20']"]. I want to remove " for my nested list. How to do this?
import ast
mystr = ["['95', '66', '137', '70', '20']"]
print(ast.literal_eval(mystr[0]))
You can use regex with re.findall
import re
lst = ["['95', '66', '137', '70', '20']"]
lst = re.findall(r'\d+', lst[0])
print(lst) # ['95', '66', '137', '70', '20']
import ast
ini_list = ["['95', '66', '137', '70', '20']"]
result = []
print(f"ini_list is: {ini_list}")
# Converting string to list
for i in ini_list:
result.append(ast.literal_eval(i))
# printing final result and its type
print(result)
Ouput
ini_list is: ["['95', '66', '137', '70', '20']"]
['95', '66', '137', '70', '20']
Same as Sam but without the import. ( not sure why the import is needed)
mystr = ["['95', '66', '137', '70', '20']"]
print(eval(mystr[0]))
import re
def return_without_quotes(x):
y = []
x = x[0]
x = x.strip(']['' ').split(', ')
for i in range(len(x)):
match = re.search('[0-9]+',x[i])
if match != None:
y.append(match.group())
x = [y]
return x
x = ["['95', '66', '137', '70', '20']"]
print(return_without_quotes(x))