# Python – Remove digits from Dictionary String Values List

• Difficulty Level : Medium
• Last Updated : 05 Sep, 2022

Given list of dictionaries with string list values, remove all the numerics from all strings.

```Input : test_dict = {'Gfg' : ["G4G is Best 4", "4 ALL geeks"],
'best' : ["Gfg Heaven", "for 7 CS"]}

Output : {'Gfg': ['GG is Best ', ' ALL geeks'],
'best': ['Gfg Heaven', 'for CS']}

Explanation : All the numeric strings are removed.

Input : test_dict = {'Gfg' : ["G4G is Best 4", "4 ALL geeks"]}

Output : {'Gfg': ['GG is Best ', ' ALL geeks']}

Explanation : All the numeric strings are removed.```

Method : Using regex + dictionary comprehension

This problem can be solved using combination of both functionalities. In this, we apply regex to replace each digit with an empty string and compile the result using dictionary comprehension.

## Python3

 `# Python3 code to demonstrate working of``# Remove digits from Dictionary String Values List``# Using loop + regex() + dictionary comprehension``import` `re` `# initializing dictionary``test_dict ``=` `{``'Gfg'` `: [``"G4G is Best 4"``, ``"4 ALL geeks"``],``             ``'is'` `: [``"5 6 Good"``],``             ``'best'` `: [``"Gfg Heaven"``, ``"for 7 CS"``]}` `# printing original dictionary``print``(``"The original dictionary is : "` `+` `str``(test_dict))` `# using dictionary comprehension to go through all keys``res ``=` `{key: [re.sub(``'\d'``, '', ele) ``for` `ele ``in` `val]``       ``for` `key, val ``in` `test_dict.items()}``        ` `# printing result``print``(``"The filtered dictionary : "` `+` `str``(res))`

Output

```The original dictionary is : {'Gfg': ['G4G is Best 4', '4 ALL geeks'], 'is': ['5 6 Good'], 'best': ['Gfg Heaven', 'for 7 CS']}
The filtered dictionary : {'Gfg': ['GG is Best ', ' ALL geeks'], 'is': ['  Good'], 'best': ['Gfg Heaven', 'for  CS']}```

Method : Without using regex

## Python3

 `# Python3 code to demonstrate working of``# Remove digits from Dictionary String Values List` `def` `fun(s):``    ``x``=``""``    ``for` `i ``in` `s:``        ``if` `not` `i.isdigit():``            ``x``+``=``i``    ``return` `x``# initializing dictionary``test_dict ``=` `{``'Gfg'` `: [``"G4G is Best 4"``, ``"4 ALL geeks"``],``            ``'is'` `: [``"5 6 Good"``],``            ``'best'` `: [``"Gfg Heaven"``, ``"for 7 CS"``]}` `# printing original dictionary``print``(``"The original dictionary is : "` `+` `str``(test_dict))` `# using dictionary comprehension to go through all keys``res``=``dict``()``for` `i ``in` `test_dict:``    ``v``=``[]``    ``for` `j ``in` `test_dict[i]:``        ``v.append(fun(j))``    ``res[i]``=``v``             ` `# printing result``print``(``"The filtered dictionary : "` `+` `str``(res))`

Output

```The original dictionary is : {'Gfg': ['G4G is Best 4', '4 ALL geeks'], 'is': ['5 6 Good'], 'best': ['Gfg Heaven', 'for 7 CS']}
The filtered dictionary : {'Gfg': ['GG is Best ', ' ALL geeks'], 'is': ['  Good'], 'best': ['Gfg Heaven', 'for  CS']}```

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n2)

Auxiliary Space: O(n)

My Personal Notes arrow_drop_up