Open In App

Python | Convert list of tuples to list of strings

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

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 3: 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 4: 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 5: 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'])

Time complexity: O(n^2), where n is the length of the input list of tuples.

Auxiliary space: O(n), where n is the length of the input list of tuples. 

Method 6: Using a for loop

Using a for loop to iterate through the elements of the list and concatenate the characters into a string

Python3




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


Output

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

Time complexity: O(n^2), where n is the total number of characters in the input list.
Auxiliary space: O(n)



Last Updated : 18 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads