Python | Remove all strings from a list of tuples
Given a list of tuples, containing both integer and strings, the task is to remove all strings from list of tuples.
Examples:
Input : [(1, 'Paras'), (2, 'Jain'), (3, 'GFG'), (4, 'Cyware')]
Output : [(1), (2), (3), (4)]
Input : [('string', 'Geeks'), (2, 225), (3, '111')]
Output : [(), (2, 225), (3,)]
Method #1: Using filter() method
# Python code to remove all strings from a list of tuples # Check function to check isinstance def check(x): return not isinstance(x, str) # creating list of tuples listOfTuples = [('string', 'Paras'), (2, 'Jain'), (3, 'GFG'), (4, 'Cyware'), (5, 'Noida')] # using filter output = ([tuple(filter(check, x)) for x in listOfTuples]) # printing output print(output) |
chevron_right
filter_none
Output:
[(), (2,), (3,), (4,), (5,)]
Method #2:
# Python code to remove all strings from a list of tuples # Creating list of tuples listOfTuples = [('string', 'Geeks'), (2, 225), (3, '111'), (4, 'Cyware'), (5, 'Noida')] output = [tuple(j for j in i if not isinstance(j, str)) for i in listOfTuples] # printing output print(output) |
chevron_right
filter_none
Output:
[(), (2, 225), (3,), (4,), (5,)]
Recommended Posts:
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove duplicate tuples from list of tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Remove empty tuples from a list
- Python | Remove tuple from list of tuples if not containing any character
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples to list of strings
- Python | Remove empty strings from list of strings
- Python | Count tuples occurrence in list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Combining tuples in list of tuples
- Python | Remove all digits from a list of strings
- Python | Remove last character in list of strings
- Python | Remove trailing/leading special characters from strings list
- Python | Remove matching tuples
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.



