1# Basix syntax:
2['delim'.join([str(elem) for elem in sublist]) for sublist in my_list]
3# Where delim is the delimiter that will separate the elements of the
4# nested lists when they are flattened to a list of strings
5
6# Note, this uses two "levels" of list comprehension
7
8# Example usage 1:
9my_list = [[1, '1', 1], [2,'2',2], [3,'3',3]]
10[' '.join([str(elem) for elem in sublist]) for sublist in my_list]
11--> ['1 1 1', '2 2 2', '3 3 3'] # List of space-delimited strings
12
13# Example usage 2:
14my_list = [[1, '1', 1], [2,'2',2], [3,'3',3]]
15['_'.join([str(elem) for elem in sublist]) for sublist in my_list]
16--> ['1_1_1', '2_2_2', '3_3_3'] # List of underscore-delimited strings
1# Python program to convert a list
2# to string using list comprehension
3
4s = ['I', 'want', 4, 'apples', 'and', 18, 'bananas']
5
6# using list comprehension
7listToStr = ' '.join(map(str, s))
8
9print(listToStr)
10
1>>> import ast
2>>> x = '[ "A","B","C" , " D"]'
3>>> x = ast.literal_eval(x)
4>>> x
5['A', 'B', 'C', ' D']
6>>> x = [n.strip() for n in x]
7>>> x
8['A', 'B', 'C', 'D']
9
1# Python program to convert a list
2# to string using list comprehension
3
4s = ['I', 'ate', 2, 'shake', 'and', 3, 'chocolates']
5
6# using list comprehension
7listToStr = ' '.join([str(element) for element in s])
8
9print(listToStr)
10