Answers for "list from string to list python"

3

python string list to list

>>> import ast
>>> x = '[ "A","B","C" , " D"]'
>>> x = ast.literal_eval(x)
>>> x
['A', 'B', 'C', ' D']
>>> x = [n.strip() for n in x]
>>> x
['A', 'B', 'C', 'D']
Posted by: Guest on May-06-2021
0

string to list

>>> names='''
...       Apple
...       Ball
...       Cat'''
>>> names
'n      Applen      Balln      Cat'
>>> names_list = [y for y in (x.strip() for x in names.splitlines()) if y]
>>> # if x.strip() is used to remove empty lines
>>> names_list
['Apple', 'Ball', 'Cat']
Posted by: Guest on December-15-2021

Code answers related to "list from string to list python"

Python Answers by Framework

Browse Popular Code Answers by Language