Change language

Python | Concatenate values ​​of a list of strings of a tuple with a string

|

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 ( 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

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