Skip to content
Related Articles

Related Articles

Improve Article

Python – Remove Punctuation Tuples

  • Last Updated : 22 Jun, 2020
Geek Week

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.




# Python3 code to demonstrate working of 
# Remove Punctuation Tuples
# Using any() + list comprehension + string.punctuation
import string
          
# initializing list
test_list = [('.', ', '), ('!', 8), (5, 6), (';', 10)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Remove Punctuation Tuples
# Using any() + list comprehension + string.punctuation
res = [idx for idx in test_list if not any(punc in idx 
                      for punc in string.punctuation)]
  
# printing result 
print("Tuples after punctuation removal : " + str(res)) 
Output :



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.




# Python3 code to demonstrate working of 
# Remove Punctuation Tuples
# Using regex() + filter() + lambda + string.punctuation
import string
import re
          
# initializing list
test_list = [('.', ', '), ('!', '8'), ('5', '6'), (';', '10')]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Remove Punctuation Tuples
# Using regex() + filter() + lambda + string.punctuation
temp = re.compile('[{}]'.format(re.escape(string.punctuation)))
res = list(filter(lambda tup: not temp.search(tup[0]), test_list))
  
# printing result 
print("Tuples after punctuation removal : " + str(res)) 
Output :
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




My Personal Notes arrow_drop_up
Recommended Articles
Page :