Change language

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

Python | Convert a list of tuples to a list of strings Python functions: Questions

Python | Convert a list of tuples to a list of strings String Variables: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method