Remove Punctuation From a Python Dictionary
Learn how to remove punctuation from dictionary values in Python while keeping keys, empty values, numbers and file input under control.
Before you start
This process runs locally in Python. You need a dictionary whose values are strings or iterables of strings, plus Python 3 installed on your computer. No cloud service or camera, microphone or image library is required. If the values are not strings, convert them to strings or handle them separately before removing punctuation.
Prepare the punctuation definition carefully. The string module provides a built-in set named string.punctuation. It covers common ASCII punctuation such as !"#$%&'()*+,-./:;<=>?@[]^_`{|}~. It does not include Unicode dashes, curly quotes or other language-specific marks unless you add them manually.
Step-by-step instructions
First, create a small function that removes selected characters from one string. Using str.translate is efficient for single-character punctuation.
import string
def clean_text(value):
translator = str.maketrans('', '', string.punctuation)
return value.translate(translator)
Next, choose whether to change the original dictionary or build a new one. Example source data could look like this: {"title": "Hello, world!", "note": "Wait... what?"}. A dictionary comprehension keeps the existing dictionary unchanged.
cleaned = {
key: clean_text(value)
for key, value in source.items()
if isinstance(value, str)
}
If you need the same keys and want to preserve non-string values, include them without cleaning.
cleaned = {}
for key, value in source.items():
if isinstance(value, str):
cleaned[key] = clean_text(value)
else:
cleaned[key] = value
To modify the dictionary in place, assign the cleaned value directly to the same key.
for key, value in source.items():
if isinstance(value, str):
source[key] = clean_text(value)
Use the manual punctuation set only when you need finer control. This is useful when you must keep punctuation that appears inside numbers, filenames or code snippets.
allowed = "._-"
punct = ''.join(
char for char in string.punctuation
if char not in allowed
)
def clean_text(value):
return value.translate(str.maketrans('', '', punct))
If you are cleaning values read from a file or a metadata export, open the file using UTF-8 and read it as text. Then apply the same cleaning step to each dictionary value.
Check the output
Print the result for a small dictionary and compare each old value with the cleaned value. Punctuation should be absent, while other characters remain in the same order.
Use an assertion for a predictable case. For an input value of "Hello, world!", the output should be "Hello world". An empty string remains empty; numbers stored as integers remain unchanged because they are not treated as text.
Check the data type after cleaning. Values that were strings remain strings. Dictionary keys are not changed by this method. If your data is large, spot-check a few entries instead of printing the whole dictionary.
Common problems and next steps
If the code fails with an attribute error, the value is probably not a string. Wrap the cleaning step in an isinstance check or convert the value with str() only when that conversion is safe for your data.
If punctuation remains after cleaning, the character is not in string.punctuation. Add the specific character to the translation set and rerun the script.
If run against a frozen snapshot with no filesystem access, test with an inline dictionary literal. This removes punctuation from values without reading or writing files.
For reusable processing, store the cleaned dictionary in a new variable rather than overwriting the source. Keep the source copy in memory or on disk so the operation is reversible.