In a sentence, I want to remove a complete string which contains special characters.
Input is ["i am in jersey1&2","this code is %bdn2*nn"]
Expected output is ["i am in","this code is"]
import re Dsp = ["i am in jersey1&2","this code is %bdn2*nn"] Dsp1 = [] for i in Dsp: Dsp = [re.sub('^\W+$'," ",j) for j in i] Dsp1.append("".join(Dsp)) I got
Dsp1 = ["i am in jersey12","this code is bdn2nn"] but the expected output is
Dsp1 = ["i am in", "this code is"]
for i in Dsp:it will iterate through all the characters in that sentence one by one, that means it wont consider individual string atifor every iteration. Why dont you try to split the sentence with delimiter as " " (blank space) then you will have a list of individual strings. Then you can check individual strings and remove the ones you dont need.