Python | List of tuples to String

Many times we can have a problem in which we need to perform interconversion between strings and in those cases, we can have a problem in which we need to convert a tuple list to raw, comma separated string. Let’s discuss certain ways in which this task can be performed.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# List of tuples to String
# using str() + strip()
  
# initialize list
test_list = [(1, 4), (5, 6), (8, 9), (3, 6)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# List of tuples to String
# using str() + strip()
res = str(test_list).strip('[]')
  
# printing result
print("Resultant string from list of tuple : " + res)

chevron_right


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 yet another way in which this task can be performed. In this, we apply the string conversion function to each element and then join the tuples using join().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# List of tuples to String
# using map() + join()
  
# initialize list
test_list = [(1, 4), (5, 6), (8, 9), (3, 6)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# List of tuples to String
# using map() + join()
res = ", ".join(map(str, test_list))
  
# printing result
print("Resultant string from list of tuple : " + res)

chevron_right


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)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.