Method # 1: Using List Comprehension + join()
A combination of the above functionality can be used to achieve the above task. In this we get a list of characters using a list comprehension and the conversion task is done by join ()
.
# Python3 code to demonstrate how it works # Merge Tuple String List of values in string # using list comprehension + join () # initialize the list test_list = [([ ’g’ , ’f’ , ’ g’ ], 1 ), ([ ’i’ , ’ s’ ], 2 ), ([ ’b’ , ’ e’ , ’s’ , ’ t’ ], 3 )] # print the original list print ( "The original list:" + str (test_list)) # Merge Tuple String List of values in string # using list comprehension + join () res = [’’ .join (i) for i, j in test_list] # print result print ( "The joined character list tuple element to string is:" + str (res)) |
Output:
The original list: [([’g’, ’f’, ’g’], 1), ([’i’, ’s’], 2), ([’ b ’,’ e ’,’ s’, ’t’], 3)]
The joined character list tuple element to string is: [’gfg’, ’is’,’ best ’]
Method # 2: Using map () + join ()
+ lambda
The task accomplished by comprehending the list in the above method can be done with map (), and lamb yes function can be used to build logic to achieve this task.
# Python3 code to demonstrate how it works # Merge Tuple String List of values into a string # using map () + join () + lambda # initialize the list test_list = [([ ’g’ , ’f’ , ’ g’ ], 1 ), ([ ’ i’ , ’s’ ], 2 ), ([ ’b’ , ’ e’ , ’s’ , ’t’ ], 3 )] # print original list print ( "The original list:" + str (test_list)) # Merge Tuple String List of values in a string # using map () + join () + lambda res = list c ode> ( map ( lambda sub: "" .join (sub [ 0 ]), test_list )) # print result print ( "The joined character list tuple element to string is:" + str (res)) |
Output:
The original list: [([’g’ , ’f’, ’g’], 1), ([’i’, ’s’], 2), ([’b’, ’e’, ’s’, ’t’], 3)]
The joined character list tuple element to string is: [’gfg’, ’is’, ’best’]
Python | Concatenate values of a list of strings of a tuple with a string Python functions: Questions
Python | Concatenate values of a list of strings of a tuple with a string String Variables: Questions