Python | Removing strings from tuple
Sometimes we can come across the issue in which we receive data in form of tuple and we just want the numbers from it and wish to erase all the strings from them. This has a useful utility in Web-Development and Machine Learning as well. Let’s discuss certain ways in which this particular task can be achieved.
Method #1 : Using list comprehension + type()
The combination of above 2 functions can be used to solve this particular problem. The list comprehension does the task of reconstruction of the modified list and type function helps us to filter the strings.
# Python3 code to demonstrate # Remove string from tuples # using list comprehension + type() # initializing list test_list = [('Geeks', 1, 2), ('for', 4, 'Geeks'), (45, 'good')] # printing original list print("The original list : " + str(test_list)) # using list comprehension + type() # Remove string from tuples res = [tuple([j for j in i if type(j) != str]) for i in test_list] # print result print("The list after string removal is : " + str(res)) |
The original list : [('Geeks', 1, 2), ('for', 4, 'Geeks'), (45, 'good')]
The list after string removal is : [(1, 2), (4, ), (45, )]
Method #2 : Using list comprehension + isinstance()
This is almost a similar method to perform this particular task but the change here is just to use the isinstance function to check for the string data type, and rest of the formulations remains mostly similar.
# Python3 code to demonstrate # Remove string from tuples # using list comprehension + isinstance() # initializing list test_list = [('Geeks', 1, 2), ('for', 4, 'Geeks'), (45, 'good')] # printing original list print("The original list : " + str(test_list)) # using list comprehension + isinstance() # Remove string from tuples res = [tuple(j for j in i if not isinstance(j, str)) for i in test_list] # print result print("The list after string removal is : " + str(res)) |
The original list : [('Geeks', 1, 2), ('for', 4, 'Geeks'), (45, 'good')]
The list after string removal is : [(1, 2), (4, ), (45, )]
Recommended Posts:
- Python | Removing duplicates from tuple
- Python | Common words among tuple strings
- Python | Get first index values in tuple of strings
- Python | Merge list of tuple into list by joining the strings
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Unpacking a Tuple in Python
- Python | Reversing a Tuple
- Python | Mean of tuple list
- Python | tuple() Function
- Python | Find whether all tuple have same length
- Python | Test if tuple is distinct
- Python | Sort lists in tuple
- Python | Unpacking tuple of lists
- Python | Convert tuple to float value
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.



