Change language

Python | List of tuples per string

|

Method # 1: Using str () + strip ()
A combination of the above functions can be used to solve this problem. In this we just convert the list to a string and strip the opening, closing square brackets of the list to represent the string.

# Python3 code to demonstrate how it works
# List of tuples to string
# using str () + strip ()

 
# initialize the list

test_list = [( 1 , 4 ), ( 5 , 6 ), ( 8 , 9 ), ( 3 , 6 )]

 
# print original list

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

  
# List of tuples per string
# using str () + strip ()

res = str (test_list) .strip ( ’[]’ )

 
# print result

print ( " Resultant string from list of tuple: " + res)

Output:

 The original list is: [(1, 4), (5, 6), (8, 9), (3, 6)] Resultant string from list of tuple: (1, 4), (5, 6), (8 , 9), (3, 6) 

Method # 2: Using map () + join ()
This is another way to accomplish this task. In this we apply a string conversion function to each element and then join the tuples using join () .

# Python3 code for demonstrations
# List of tuples in a string
# using map () + join ()

 
# initialize the list

test_list = [( 1 , 4 ), ( 5 , 6 ), ( 8 , 9 ), ( 3 , 6 )]

 
# print original list

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

  
# List of tuples per string
# using map () + join ()

res = "," . join ( map ( str , test_list))

 
# print result

print ( "Resultant string from list of tuple:" + res)

The output:

 The original list is: [(1, 4), (5, 6), (8, 9), (3, 6) ] Resultant string from list of tuple: (1, 4), (5, 6), (8, 9), (3, 6) 

Python | List of tuples per string Python functions: Questions

Python | List of tuples per 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