Python | Merge list of tuple into list by joining the strings

Sometimes, we are required to convert list of tuples into a list by joining two element of tuple by a special character. This is usually with the cases with character to string conversion. This type of task is usually required in the development domain to merge the names into one element. Let’s discuss certain ways in which this can be performed.

Let’s try to understand it better with code examples.

Method 1: Using list comprehension and join()



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to convert list of tuple into list
# by joining elements of tuple
  
# Input list initialisation
Input = [('Hello', 'There'), ('Namastey', 'India'), ('Incredible', 'India')]
  
# using join and list comprehension
Output = ['_'.join(temp) for temp in Input]
  
# printing output
print(Output)

chevron_right


Output:

['Hello_There', 'Namastey_India', 'Incredible_India']

 
Method 2: Using map and join()

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to convert list of tuple into list
# by joining elements of tuple
  
# Input list initialisation
Input = [('Hello', 'There'), ('Namastey', 'India'), ('Incredible', 'India')]
  
# using map and join
Output = list(map('_'.join, Input))
  
# printing output
print(Output)

chevron_right


Output:

['Hello_There', 'Namastey_India', 'Incredible_India']


My Personal Notes arrow_drop_up


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.