Python | Convert list of tuples to list of strings
The interconversion between datatypes is quite useful utility and many articles have been written to perform the same. This article discusses the interconversion between a tuple of characters to individual strings. This kind of interconversion is useful in Machine Learning in which we need to give the input to train model in a specific format. Let’s discuss certain ways in which this can be done.
Method #1 : Using list comprehension + join()
The list comprehension performs the task of iterating the entire list of tuples and join function performs the task of aggregating the elements of tuple into a one list.
# Python3 code to demonstrate # conversion of list of tuple to list of list # using list comprehension + join() # initializing list test_list = [( 'G' , 'E' , 'E' , 'K' , 'S' ), ( 'F' , 'O' , 'R' ), ( 'G' , 'E' , 'E' , 'K' , 'S' )] # printing the original list print ( "The original list is : " + str (test_list)) # using list comprehension + join() # conversion of list of tuple to list of list res = [''.join(i) for i in test_list] # printing result print ( "The list after conversion to list of string : " + str (res)) |
Output :
The original list is : [(‘G’, ‘E’, ‘E’, ‘K’, ‘S’), (‘F’, ‘O’, ‘R’), (‘G’, ‘E’, ‘E’, ‘K’, ‘S’)]
The list after conversion to list of string : [‘GEEKS’, ‘FOR’, ‘GEEKS’]
Method #2 : Using map() + join()
The task performed by the list comprehension can be performed by the map function which can perform extension of logic of one tuple to all tuple in list.
# Python3 code to demonstrate # conversion of list of tuple to list of list # using map() + join() # initializing list test_list = [( 'G' , 'E' , 'E' , 'K' , 'S' ), ( 'F' , 'O' , 'R' ), ( 'G' , 'E' , 'E' , 'K' , 'S' )] # printing the original list print ( "The original list is : " + str (test_list)) # using map() + join() # conversion of list of tuple to list of list res = list ( map (''.join, test_list)) # printing result print ( "The list after conversion to list of string : " + str (res)) |
Output :
The original list is : [(‘G’, ‘E’, ‘E’, ‘K’, ‘S’), (‘F’, ‘O’, ‘R’), (‘G’, ‘E’, ‘E’, ‘K’, ‘S’)]
The list after conversion to list of string : [‘GEEKS’, ‘FOR’, ‘GEEKS’]
Recommended Posts:
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples to list of list
- Python | Convert string tuples to list tuples
- Python | Convert list of tuples into list
- Python | Convert String to list of tuples
- Python | Convert list of tuples into digits
- Python | Convert dictionary to list of tuples
- Python | Convert a list of Tuples into Dictionary
- Python | Convert list elements to bi-tuples
- Python | Convert list of tuples to dictionary value lists
- Python | Convert List of lists to list of Strings
- Python | Remove all strings from a list of tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert case of elements in a list of strings
- Python | Convert list of strings and characters to list of characters
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.