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()
# 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) |
Output:
['Hello_There', 'Namastey_India', 'Incredible_India']
Method 2: Using map and join()
# 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) |
Output:
['Hello_There', 'Namastey_India', 'Incredible_India']
Please Login to comment...