Python | Join tuple elements in a list

Now days, data is something that is backbone of any Machine Learning technique. The data can come in any form and its sometimes required to be extracted out to be processed. This article deals with the issue of extracting information which is present in tuples in list. Let’s discuss certain ways in which this can be performed.

Method #1 : Using join() + list comprehension
The join function can be used to join each tuple elements with each other and list comprehension handles the task of iterating through the tuples.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# joining tuple elements
# using join() + list comprehension
  
# initializing tuple list 
test_list = [('geeks', 'for', 'geeks'),
             ('computer', 'science', 'portal')]
  
# printing original list
print ("The original list is : " + str(test_list))
  
# using join() + list comprehension
# joining tuple elements 
res = [' '.join(tups) for tups in test_list]
  
# printing result 
print ("The joined data is : " +  str(res))

chevron_right


Output:

The original list is : [(‘geeks’, ‘for’, ‘geeks’), (‘computer’, ‘science’, ‘portal’)]
The joined data is : [‘geeks for geeks’, ‘computer science portal’]

Output :



 
Method #2 : Using map() + join()
The functionality of list comprehension in the above method can also be done using the map function. This reduces the size of the code increasing its readability.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# joining tuple elements
# using join() + map()
  
# initializing tuple list 
test_list = [('geeks', 'for', 'geeks'),
             ('computer', 'science', 'portal')]
  
# printing original list
print ("The original list is : " + str(test_list))
  
# using join() + map()
# joining tuple elements 
res = list(map(" ".join, test_list))
  
# printing result 
print ("The joined data is : " +  str(res))

chevron_right


Output:

The original list is : [(‘geeks’, ‘for’, ‘geeks’), (‘computer’, ‘science’, ‘portal’)]
The joined data is : [‘geeks for geeks’, ‘computer science portal’]



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.