Skip to content
Related Articles

Related Articles

Python | Convert list of tuples to list of strings

View Discussion
Improve Article
Save Article
  • Last Updated : 29 Sep, 2022
View Discussion
Improve Article
Save Article

The interconversion between datatypes is a 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 the 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 the join function performs the task of aggregating the elements of tuples into one list. 

Python3




# 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 an extension of the logic of one tuple to all tuples in the list. 

Python3




# 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']

Method: Using format specifier

Python3




l=test_list = [('G', 'E', 'E', 'K', 'S'), ('F', 'O', 'R'),('G', 'E', 'E', 'K', 'S')]
 
x=[('{}'*len(t)).format(*t).strip() for t in l]
print(x)

Output

['GEEKS', 'FOR', 'GEEKS']

Method: Using enumerate function

Python3




test_list = [('G', 'E', 'E', 'K', 'S'), ('F', 'O', 'R'),
             ('G', 'E', 'E', 'K', 'S')]
 
res = [''.join(i) for a, i in enumerate(test_list)]
 
print("The list after conversion to list of string : " + str(res))

Output

The list after conversion to list of string : ['GEEKS', 'FOR', 'GEEKS']

Method: Using functools.reduce method

Python




# Python3 code to demonstrate
# conversion of list of tuple to list of list
# using list comprehension + join()
from functools import reduce
# 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 = reduce(lambda a, b:  a + [''.join(b)] if type(a)
             != tuple else [''.join(a)]+[''.join(b)], test_list)
# printing result
print("The list after conversion to list of string : ", 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'])

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!