Search code examples
stringpython-3.xpunctuation

How to remove punctuations in a dictionary


I have a dictionary where key is string and values are a list of strings. I tried to remove the punctuations using strings.punctuations from import strings module.

>>> dat = {'2008':['what!','@cool','#fog','@dddong'],'2010':['hey','@cute']}
>>> 

>>> def remove_punct(data):
...     import string
...     punct = string.punctuation
...     rpunct = punct.replace('@',"") # withold @
...     for k,v in data.items():
...         for word in data[k]:
...             word = word.strip(rpunct)
...     return data
... 
>>> remove_punct(dat)
{'2008': ['what!', '@cool', '#fog', '@dddong'], '2010': ['hey', '@cute']}

Why won't I get the result with # and ! removed?

Do I have to define the dictionary again, after word.strip(rpunct)...?


Solution

  • You are not actually modifying data. You need to either directly modify data or create a new dictionary and fill this with the new data:

    >>> dat = {'2008':['what!','@cool','#fog','@dddong'],'2010':['hey','@cute']}
    >>> 
    >>> def remove_punct(data):
    ...     import string
    ...     new_data = {} # the data we will return
    ...     punct = string.punctuation
    ...     rpunct = punct.replace('@',"") # withold @
    ...     for k,v in data.items():
    ...         new_data[k] = []
    ...         for word in data[k]:
    ...             new_data[k].append(word.strip(rpunct))
    ...     return new_data
    ... 
    >>> remove_punct(dat)
    {'2008': ['what', '@cool', 'fog', '@dddong'], '2010': ['hey', '@cute']}
    

    Or in fewer lines:

    >>> from string import punctuation
    >>> rpunct = punctuation.replace('@',"") # withold @
    >>> new_data = {k: [word.strip(rpunct) for word in dat[k]] for k in dat}