Python | Convert mixed data types tuple list to string list
Sometimes, while working with records, we can have a problem in which we need to perform type conversion of all records into a specific format to string. This kind of problem can occur in many domains. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + tuple() + str() + generator expression
The combination of the above functions can be used to perform this task. In this, we extract each tuple element using generation expression and perform the conversion using str(). The iteration of each tuple is done by list comprehension.
Python3
test_list = [( 'gfg' , 1 , True ), ( 'is' , False ), ( 'best' , 2 )]
print ("The original list : " + str (test_list))
res = [ tuple ( str (ele) for ele in sub) for sub in test_list]
print ("The tuple list after conversion : " + str (res))
|
Output :
The original list : [('gfg', 1, True), ('is', False), ('best', 2)]
The tuple list after conversion : [('gfg', '1', 'True'), ('is', 'False'), ('best', '2')]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using map() + tuple() + str() + list comprehension
The combination of the above functions can be used to perform this task. In this, we perform the task performed by the generator expression above using map() method.
Python3
test_list = [( 'gfg' , 1 , True ), ( 'is' , False ), ( 'best' , 2 )]
print ("The original list : " + str (test_list))
res = [ tuple ( map ( str , sub)) for sub in test_list]
print ("The tuple list after conversion : " + str (res))
|
Output :
The original list : [('gfg', 1, True), ('is', False), ('best', 2)]
The tuple list after conversion : [('gfg', '1', 'True'), ('is', 'False'), ('best', '2')]
Time complexity: O(n*m).
Auxiliary Space: O(n*m).
Method #3: Using for loop and list.append()
Python3
test_list = [( 'gfg' , 1 , True ), ( 'is' , False ), ( 'best' , 2 )]
print ( "The original list : " + str (test_list))
res = []
for sub in test_list:
res.append( tuple ( str (ele) for ele in sub))
print ( "The tuple list after conversion : " + str (res))
|
Output
The original list : [('gfg', 1, True), ('is', False), ('best', 2)]
The tuple list after conversion : [('gfg', '1', 'True'), ('is', 'False'), ('best', '2')]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#4: Using recursive method
Python3
def convert_to_string_list(lst):
if not lst:
return []
if type (lst[ 0 ]) = = tuple :
return [ tuple ( map ( str , lst[ 0 ]))] + convert_to_string_list(lst[ 1 :])
return [ str (lst[ 0 ])] + convert_to_string_list(lst[ 1 :])
test_list = [( 'gfg' , 1 , True ), ( 'is' , False ), ( 'best' , 2 )]
print ( "The original list : " + str (test_list))
result = convert_to_string_list(test_list)
print ( "The tuple list after conversion : " + str (result))
|
Output
The original list : [('gfg', 1, True), ('is', False), ('best', 2)]
The tuple list after conversion : [('gfg', '1', 'True'), ('is', 'False'), ('best', '2')]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#5: Using itertools.chain()
- Initialize a list of tuples, test_list, with mixed data types.
- Print the original list using the print() function.
- Convert the tuples of the mixed test_list into a string list using the map() function, itertools.chain() function, and str() function:
a. Use map(list, test_list) to convert the tuples into lists of their elements.
b. Use itertools.chain() to combine all the elements of the lists into a single iterable object.
c. Use map(str, x) to convert each element of the iterable object to a string.
d. Use tuple() to convert the iterable object back into tuples.
e. Use list comprehension to create a new list of tuples that have their elements converted to strings.
- Store the converted string list in res.
- Print the converted tuple list using the print() function.
Python3
import itertools
test_list = [( 'gfg' , 1 , True ), ( 'is' , False ), ( 'best' , 2 )]
print ( "The original list : " + str (test_list))
res = [ tuple ( map ( str , x)) for x in map ( list , test_list)]
print ( "The tuple list after conversion : " + str (res))
|
Output
The original list : [('gfg', 1, True), ('is', False), ('best', 2)]
The tuple list after conversion : [('gfg', '1', 'True'), ('is', 'False'), ('best', '2')]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using list comprehension and join()
This program converts a list of tuples with mixed data types to a list of tuples with string data types using list comprehension and tuple conversion. The output list is then printed to the console.
Python3
test_list = [( 'gfg' , 1 , True ), ( 'is' , False ), ( 'best' , 2 )]
res = [ tuple ( str (e) for e in sub) for sub in test_list]
print ( "The tuple list after conversion : " + str (res))
|
Output
The tuple list after conversion : [('gfg', '1', 'True'), ('is', 'False'), ('best', '2')]
Time complexity: O(n*m)
Where n is the length of the input list and m is the length of the longest tuple in the list.
Auxiliary space: O(n*m)
Since we are creating a new list of tuples with the same number of elements as the input list, and each tuple has the same number of items as the longest tuple in the input list.
Last Updated :
27 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...