ankitpandey ankitpandey - 4 months ago 38
Python Question

Remove additional quotes from python list

I have a list with below pattern and i want to get rid of

"
which is present at the beginning and end of each sub list. I tried replace, strip but they are not the attribute of list and therefore gives
AttributeError
.

lst = [["'123', 'Name1', 'Status1'"], ["'234', 'Name2', 'Status2'"]]


I am looking for below as my final result:

lst = [['123', 'Name1', 'Status1'], ['234', 'Name2', 'Status2']]


Please suggest how to remove double quotes from each sub list.

Answer

You can use shlex.split after removing commas with replace:

import shlex

lst = [["'123', 'Name1', 'Status1'"], ["'234', 'Name2', 'Status2'"]]
r = [shlex.split(x[0].replace(',', '')) for x in lst]
# [['123', 'Name1', 'Status1'], ['234', 'Name2', 'Status2']]