Python | Convert a list of tuples to a list of strings



Method # 1: Using list comprehension + join()
List comprehension performs the task of iterating through the entire list of tuples, and the join function performs the task of joining the elements of a tuple into one list.

# Python3 demo code
# convert a tuple list to a list of list
# using a list comprehension + join ()

 
# initializing list

test_list = [( `G` , ` E` , `E` , `K` , `S` ), ( ` F` , `O` , `R` ),

  ( `G` , ` E` , `E` , `K` , ` S` )]

 
# print original list

print ( "The original list is:" + str (test_list))

  
# using a list comprehension + join ()
# converting a list tuple to the list of list

res = [` `.join (i) for i in test_list]

 
# print 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 li st of string: [`GEEKS`, `FOR`, `GEEKS`]

Method # 2: Using map () + join ()
The task of comprehending a list can be accomplished by the map function, which can expand the logic of one tuple to all tuples in the list.

# Python3 demo code
# convert a list of a tuple to a list of a list
# using map ( ) + join ()

 
# initializing list

test_list = [( ` G` , `E` , `E` , ` K` , `S` ), ( `F` , ` O` , `R` ),

( `G` , `E` , ` E ` , ` K` , `S` )]

  
# print original list

print ( "The original list is:" + str (test_list))

 
# using map () + join ()
# convert a tuple list to a list of list

res = list ( map (`` .join, test_list))

 
# print 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 a fter conversion to list of string: [`GEEKS`, `FOR`, `GEEKS`]