Sometimes, while working with Python tuples, we can have a problem in which we need to perform the removal of all the tuples which contain punctuation in tuples. This kind of problem can occur in data filtering applications. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [(‘.’, ‘, ‘), (‘!’, 8)]
Output : 
Input : test_list = [(1, 3), (3, 8)]
Output : [(1, 3), (3, 8)]
Method #1 : Using
any() + list comprehension + string.punctuation
The combination of above functions can be used to solve this problem. In this, we perform the task of identifying punctuation using string.punctuations, and any() is used to test if the elements belong to any of punctuation.
The original list is : [('.', ', '), ('!', 8), (5, 6), (';', 10)] Tuples after punctuation removal : [(5, 6)]
Method #2 : Using
regex() + filter() + lambda + string.punctuation
The combination of above functions can be used to solve this problem. In this, we perform the task of identification of punctuations using regex expressions and filtering using filter() + lambda. Has limitation of working only on strings and for checking on particular index.
The original list is : [('.', ', '), ('!', '8'), ('5', '6'), (';', '10')] Tuples after punctuation removal : [('5', '6')]
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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course