convert string representation of list to list python code example
Example 1: python convert list of lists to list of strings
['delim'.join([str(elem) for elem in sublist]) for sublist in my_list]
my_list = [[1, '1', 1], [2,'2',2], [3,'3',3]]
[' '.join([str(elem) for elem in sublist]) for sublist in my_list]
--> ['1 1 1', '2 2 2', '3 3 3']
my_list = [[1, '1', 1], [2,'2',2], [3,'3',3]]
['_'.join([str(elem) for elem in sublist]) for sublist in my_list]
--> ['1_1_1', '2_2_2', '3_3_3']
Example 2: string list into list pandas
import ast
ini_list = "[1, 2, 3, 4, 5]"
res = ast.literal_eval(ini_list)
print ("final list", res)
print (type(res))
Example 3: how do i convert a list to a string in python
lines2 = " ".join(lines)
file.write(lines2)