ankitpandey ankitpandey - 1 year ago 170
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

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 Source

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']]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download