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 co de> 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’ co de> , 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