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 - Join Tuples to Integers in Tuple List
- Python | Sort tuple list by Nth element of tuple
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python - Raise elements of tuple as power to another tuple
- Python | Replace tuple according to Nth tuple element
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python | Ways to join pair of elements in list
- Python - Consecutive K elements join in List
- Python - Group first elements by second elements in Tuple list
- Python | Join cycle in list
- Python | Pair and combine nested list to tuple list
- 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 | Convert list to indexed tuple list
- Python | Convert Integral list to tuple list
- Python | Convert mixed data types tuple list to string list
- Python | Convert List of Dictionary to 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.

