Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Convert mixed data types tuple list to string list

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




# Python3 code to demonstrate working of
# Convert tuple mixed list to string list
# using list comprehension + tuple() + str() + generator expression
 
# initialize list
test_list = [('gfg', 1, True), ('is', False), ('best', 2)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Convert tuple mixed list to string list
# using list comprehension + tuple() + str() + generator expression
res = [tuple(str(ele) for ele in sub) for sub in test_list]
 
# printing result
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




# Python3 code to demonstrate working of
# Convert tuple mixed list to string list
# using map() + tuple() + str() + list comprehension
 
# initialize list
test_list = [('gfg', 1, True), ('is', False), ('best', 2)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Convert tuple mixed list to string list
# using map() + tuple() + str() + list comprehension
res = [tuple(map(str, sub)) for sub in test_list]
 
# printing result
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




# initialize list
test_list = [('gfg', 1, True), ('is', False), ('best', 2)]
 
# printing original list
print("The original list : " + str(test_list))
 
res = []
for sub in test_list:
    res.append(tuple(str(ele) for ele in sub))
 
# printing result
print("The tuple list after conversion : " + str(res))
#This code is contributed by Vinay Pinjala.


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




# Python3 code to demonstrate working of
# Convert tuple mixed list to string list
# using recursive method
 
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:])
 
  # initialize list
test_list = [('gfg', 1, True), ('is', False), ('best', 2)]
 
# printing original list
print("The original list : " + str(test_list))
 
result = convert_to_string_list(test_list)
 
# printing result
print("The tuple list after conversion : " + str(result))
#this code contributed by tvsk


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()

  1. Initialize a list of tuples, test_list, with mixed data types.
  2. Print the original list using the print() function.
  3. 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.
  4. Store the converted string list in res.
  5. Print the converted tuple list using the print() function.

Python3




import itertools
# initialize list
test_list = [('gfg', 1, True), ('is', False), ('best', 2)]
# printing original list
print("The original list : " + str(test_list))
# Convert tuple mixed list to string list
# using map() + itertools.chain() + str()
res = [tuple(map(str, x)) for x in map(list, test_list)]
# printing result
print("The tuple list after conversion : " + str(res))
# This code is contributed by Jyothi pinjala


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. 

RELATED ARTICLES

Most Popular

Recent Comments