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 | Get first index values in tuple of strings
- Python | Sort tuple list by Nth element of tuple
- Python | Merge list of tuple into list by joining the strings
- Python | Reversing a Tuple
- Unpacking a Tuple in Python
- Python | tuple() Function
- Python | Find whether all tuple have same length
- Python | Split tuple into groups of n
- Python | Convert a list into a tuple
- Python | Difference Between List and Tuple
- Python | Remove Consecutive tuple according to key
- Python | Unpacking tuple of lists
- Python | Program to convert a tuple to a string
- Python | Convert a list into tuple of lists
- Python | Sort a tuple by its float element
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.



