Given a list of tuples, the task is to remove all those tuples which do not contain any character value.
Example:
Input: [(', ', 12), ('...', 55),
('-Geek', 115), ('Geeksfor', 115),]
Output: [('-Geek', 115), ('Geeksfor', 115)]
Method #1 : Using list comprehension
# Python code to remove all those # elements from list of tuple # which does not contains any alphabet. # List initialization List = [(', ', 12), ('Paras', 5), ('jain.', 11), ('...', 55), ('-Geek', 115), ('Geeksfor', 115), (':', 63), ('Data', 3), ('-', 15), ('Structure', 32), ('Algo', 80),] # Using list comprehension out = [(a, b) for a, b in List if any(c.isalpha() for c in a)] # Printing output print(out) |
[(‘Paras’, 5), (‘jain.’, 11), (‘-Geek’, 115), (‘Geeksfor’, 115), (‘Data’, 3), (‘Structure’, 32), (‘Algo’, 80)]
Method #2 : Using Regex
# Python code to remove all those # elements from list of tuple # which does not contains any alphabet. # List initialization List = [(', ', 12), ('Paras', 5), ('jain.', 11), ('...', 55), ('-Geek', 115), ('Geeksfor', 115), (':', 63), ('Data', 3), ('-', 15), ('Structure', 32), ('Algo', 80),] # Importing import re # Using regex out = [t for t in List if re.search(r'\w', t[0])] # Printing output print(out) |
[(‘Paras’, 5), (‘jain.’, 11), (‘-Geek’, 115), (‘Geeksfor’, 115), (‘Data’, 3), (‘Structure’, 32), (‘Algo’, 80)]
Method 3 : Using Filter and lambda
# Python code to remove all those # elements from list of tuple # which does not contains any alphabet. # List initialization List = [(', ', 12), ('Paras', 5), ('jain.', 11), ('...', 55), ('-Geek', 115), ('Geeksfor', 115), (':', 63), ('Data', 3), ('-', 15), ('Structure', 32), ('Algo', 80),] # Using filter out = filter(lambda x:any(c.isalpha() for c in x[0]), List) # Converting in list out = list(out) # Printing output print(out) |
[(‘Paras’, 5), (‘jain.’, 11), (‘-Geek’, 115), (‘Geeksfor’, 115), (‘Data’, 3), (‘Structure’, 32), (‘Algo’, 80)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove tuples having duplicate first value from given list of tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python - Convert Tuple value list to List of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Combining tuples in list of tuples
- Python | Convert string tuples to list tuples
- Python - Filter all uppercase characters Tuples from given list of tuples
- Python program to find Tuples with positive elements in List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python | Remove List elements containing given String character
- Python | Group by matching second tuple value in list of tuples
- Python - Combinations of sum with tuples in tuple list
- Python | Reverse each tuple in a list of tuples
- Python | Swap tuple elements in list of tuples
- Python - Convert List of Lists to Tuple of Tuples
- Python - Join Tuples to Integers in Tuple List
- Python | How to Concatenate tuples to nested 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.
Improved By : nidhi_biet

