Yan Long Yan Long - 1 year ago 86
Python Question

How to remove non alphanumeric characters in a dictionary using python

I have a dictionary like:

{...., '5':24, '/':25', 'a':26', ....}

How can I remove the punctuations and digits in order to only keep the alphanumeric characters in the dictionary?

I can do a traditional way to remove those items by indexing the key, but is there an efficient way to remove them by using regular expression?

Answer Source

with dictionary d:

d = {key: value for key, value in d.items() if key.isalpha()}