17

I have a list like

['hello', '...', 'h3.a', 'ds4,']

this should turn into

['hello', 'h3a', 'ds4']

and i want to remove only the punctuation leaving the letters and numbers intact. Punctuation is anything in the string.punctuation constant. I know that this is gunna be simple but im kinda noobie at python so...

Thanks, giodamelio

giodamelio
  • 5,465
  • 14
  • 44
  • 72

6 Answers6

28

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']
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
9

Use string.translate:

>>> import string
>>> test_case = ['hello', '...', 'h3.a', 'ds4,']
>>> [s.translate(None, string.punctuation) for s in test_case]
['hello', '', 'h3a', 'ds4']

For the documentation of translate, see http://docs.python.org/library/string.html

Josh Bleecher Snyder
  • 8,262
  • 3
  • 35
  • 37
3

In python 3+ use this instead:

import string
s = s.translate(str.maketrans('','',string.punctuation))
florex
  • 943
  • 7
  • 9
2
import string

print ''.join((x for x in st if x not in string.punctuation))

ps st is the string. for the list is the same...

[''.join(x for x in par if x not in string.punctuation) for par in alist]

i think works well. look at string.punctuaction:

>>> print string.punctuation
!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~
Ant
  • 5,151
  • 2
  • 26
  • 43
1

To make a new list:

[re.sub(r'[^A-Za-z0-9]+', '', x) for x in list_of_strings]
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
0

Just be aware that string.punctuation works in English, but may not work for other languages with other punctuation marks.

You could add them to a list LIST_OF_LANGUAGE_SPECIFIC_PUNCTUATION and then concatenate it to string.punctuation to get a fuller set of punctuation.

punctuation =  string.punctuation + [LIST_OF_LANGUAGE_SPECIFIC_PUNCTUATION]