Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using map() + generator expression + join() + isdigit()
This task can be performed using a combination of the above functions. In this, we join the numbers using join and construct a string integers. The map() is used to apply logic to each element in list.
Python3
# Python3 code to demonstrate working of# Convert List of lists to list of Strings# using list comprehension + join()# initialize listtest_list = [["g", "f", "g"], ["i", "s"], ["b", "e", "s", "t"]]# printing original listprint("The original list : " + str(test_list))# Convert List of lists to list of Strings# using list comprehension + join()res = [''.join(ele) for ele in test_list]# printing resultprint("The String of list is : " + str(res)) |
The original list : ['[1, 4]', '[5, 6]', '[7, 10]'] List after performing conversion : ['14', '56', '710']
Method #2: Using eval() + list comprehension
The combination of above functionalities can be used to perform this task. In this, eval() interprets each strings as list and then we can convert that list to strings using join(). List comprehension is used to iterate through the list.
Python3
# Python3 code to demonstrate working of# Convert List of lists to list of Strings# using map() + join()# initialize listtest_list = [["g", "f", "g"], ["i", "s"], ["b", "e", "s", "t"]]# printing original listprint("The original list : " + str(test_list))# Convert List of lists to list of Strings# using map() + join()res = list(map(''.join, test_list))# printing resultprint("The String of list is : " + str(res)) |
The original list : ['[1, 4]', '[5, 6]', '[7, 10]'] List after performing conversion : ['14', '56', '710']
Method #3: Using enumerate function
Python3
test_list = ["[1, 4]", "[5, 6]", "[7, 10]"]res = [''.join(str(b) for b in eval(a)) for i, a in enumerate(test_list)]print(res) |
['14', '56', '710']


