Removing Punctuation From Python List Items

Assuming that your initial list is stored in a variable x, you can use this:

>>> x = [''.join(c for c in s if c not in string.punctuation) for s in x]
>>> print(x)
['hello', '', 'h3a', 'ds4']

To remove the empty strings:

>>> x = [s for s in x if s]
>>> print(x)
['hello', 'h3a', 'ds4']

Leave a Comment