![]() |
VOOZH | about |
We are given dictionary we need to remove digits from the dictionary string value list. For example, we are given a dictionary that contains strings values d = {'key1': ['abc1', 'def2', 'ghi3'], 'key2': ['xyz4', 'lmn5']} we need to remove digits from dictionary so that resultant output becomes {'key1': ['abc', 'def', 'ghi'], 'key2': ['xyz', 'lmn']}.
re.sub()This method uses regular expressions to remove digits and list comprehension to apply it to each string in the list.
{'key1': ['abc', 'def', 'ghi'], 'key2': ['xyz', 'lmn']}
Explanation:
str.translate() and str.maketrans()This method uses str.translate() in combination with str.maketrans() to remove digits from each string.
{'key1': ['abc', 'def', 'ghi'], 'key2': ['xyz', 'lmn']}
Explanation:
str.join()This method iterates through each character in the string and removes the digits using a list comprehension.
{'key1': ['abc', 'def', 'ghi'], 'key2': ['xyz', 'lmn']}
Explanation:
str.replace()We can use str.replace() in a loop to replace each digit with an empty string.
{'key1': ['abc', 'def', 'ghi'], 'key2': ['xyz', 'lmn']}
Explanation:
replace() method is used to remove specific digits (1, 2, 3, 4, 5) from each string in the dictionary's lists by replacing them with an empty string.