Reputation: 31
How would I delete an unknown character from a list of strings?
For example my list is ['hi', 'h@w', 'are!', 'you;', '25']
and I want to delete all the characters that are not words or numbers?
How would I do this?
Upvotes: 0
Views: 1015
Reputation: 27577
Use re.sub
:
from re import sub
lst = ['hi', 'h@w', 'are!', 'you;', '25']
lst = [sub('[^\w]', '', i) for i in lst]
print(lst)
Output:
['hi', 'hw', 'are', 'you', '25']
Explanation:
This line: sub('[^\w]', '', i)
tells python to replace all the substrings of pattern "[^\w]"
with an empty string, ""
inside the i
string, and return the result.
The pattern [^\w]
finds all the characters in a string that are not letters or numbers.
Upvotes: 0