Removing punctuation/symbols from a list with Python except periods, commas
Question: In Python, I need to remove almost all punctuation from a list but save
periods and commas. Should I create a function to do this or a variable?
Basically I want to delete all symbols except letters (I've already converted
uppercase letters to lowercase) and periods and commas (and maybe
apostrophes).

    
    
    #Clean tokens up (remove symbols except ',' and '.')
    
    def depunctuate()
       clean_tokens = []
    
       for i in lc_tokens:
           if (i not in [a-z.,])
           ...
    


Answer: You can build a set of unwanted punctuation from
[`string.punctuation`](https://docs.python.org/2/library/string.html#string.punctuation)
\- which provides a string containing punctuation, and then use a _list
comprehension_ to filter out the letters contained in the set:

    
    
    import string
    
    to_delete = set(string.punctuation) - {'.', ','} # remove comma and fullstop
    clean_tokens = [x for x in lc_tokens if x not in to_delete]
    

