Python | Convert List of String List to String List

Sometimes while working in Python, we can have problems of interconversion of data. This article talks about 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 combination of 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Convert List of String List to String List
# using map() + generator expression + join() + isdigit()
  
# helper function 
def convert(sub):
    return "".join(ele if ele.isdigit() else "" for ele in sub)
  
# initialize list 
test_list = ["[1, 4]", "[5, 6]", "[7, 10]"]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Convert List of String List to String List
# using map() + generator expression + join() + isdigit()
res = list(map(convert, test_list))
  
# printing result
print("List after performing conversion : " + str(res))

chevron_right


Output :



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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Convert List of String List to String List
# using eval() + list comprehension
  
# initialize list 
test_list = ["[1, 4]", "[5, 6]", "[7, 10]"]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Convert List of String List to String List
# using eval() + list comprehension
res = [''.join(str(b) for b in eval(a)) for a in test_list]
  
# printing result
print("List after performing conversion : " + str(res))

chevron_right


Output :

The original list : ['[1, 4]', '[5, 6]', '[7, 10]']
List after performing conversion : ['14', '56', '710']



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.