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.
# 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)) |
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.
# 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)) |
The original list is : [(‘geeks’, ‘for’, ‘geeks’), (‘computer’, ‘science’, ‘portal’)]
The joined data is : [‘geeks for geeks’, ‘computer science portal’]
Recommended Posts:
- Python | Pandas str.join() to join string/list elements with passed delimiter
- Python | Ways to join pair of elements in list
- Python | Count the elements in a list until an element is a Tuple
- Python | Count occurrence of all elements of list in a tuple
- Python | Binary Group Tuple list elements
- Python | Swap tuple elements in list of tuples
- Python | Sort tuple list on basis of difference of elements
- Python | Sort tuple list by Nth element of tuple
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Merge list of tuple into list by joining the strings
- Python | Pair and combine nested list to tuple list
- Python | Convert Integral list to tuple list
- Python | Convert list to indexed tuple list
- Python | Join cycle in list
- Python | Mean of tuple list
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.



