Change language

Python | Convert a list of tuples of mixed data types to a list of strings

|

Method # 1: Using a comprehension list + tuple () + str () + expression generator
A combination of the above functions can be used to accomplish this task. In this, we extract each element of the tuple using a generation expression and perform the conversion using str (). Iterate for each tuple by comprehending the list.

# Python3 code to demonstrate how it works
# Convert mixed list of tuples to list of strings
# use comprehension list + tuple () + str () + generator expression

 
# initialize the list

test_list = [( ’gfg’ , 1 , True ), ( ’ is’ , False ), ( ’best’  , 2 )]

 
# print original list

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

  
# Convert mixed list of tuples to list of strings
# use comprehension list + tuple () + str () + generator expression

res = [ tuple ( str (ele) for ele in sub) for sub in test_list]

 
# print 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’)] 

Method # 2: Using map () + tuple () + str ( ) + List Comprehension
A combination of the above functions can be used to accomplish this task. In this we accomplish the task performed by the generator expression above using map ().

# Python3 code to demonstrate how it works
# Convert a mixed list of tuples to a list of strings
# using map () + tuple () + str () + list comprehension

 
# initialize the list

test_list = [( ’gfg’ , 1 , True ), ( ’is’ , False ), ( ’best’ , 2 )]

 
# print original list

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

 
# Convert mixed list of tuples to list of strings
# using map () + tuple () + str () + list comprehension

res = [ tuple ( map ( str , sub)) for sub in test_list]

  
# print 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’)] 

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

Python | Convert a list of tuples of mixed data types 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