I already know this is a really dumb question. I tried looking up my answer but I barely know what to ask. (sorry if the title is a little vague). But Here I go. I have a list of words. I'm wanting to get rid of the bad characters in that list.
List = ["I?", "Can", "!Not", "Do.", "It"]
BadChars = ["?", "!", "."]
for word in List:
for char in word:
if char in BadChars:
char = ""
print(List)
Again, I know this is very simple, but I just can't figure it out. Forgive me.
EDIT: Btw, it's not giving me an error. It's just returning the List untouched.
You could use the
replace
method, for each char for each word:A regex may be used as well:
You can use a generator expression that iterates over each character in a string and retains characters that are not in
BadChars
:This returns:
Just use strip method to remove BadChars from List