Python - Remove digits from Dictionary String Values List
Last Updated :
28 Jan, 2025
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']}.
Using re.sub()
This method uses regular expressions to remove digits and list comprehension to apply it to each string in the list.
Python
import re
# Initial dictionary with lists of strings containing digits
d = {'key1': ['abc1', 'def2', 'ghi3'], 'key2': ['xyz4', 'lmn5']}
# Iterate through the dictionary to process each list of strings
for key in d:
# Use list comprehension to apply re.sub() to each string in the list
# re.sub(r'\d+', '', item) removes all digits from each string
d[key] = [re.sub(r'\d+', '', item) for item in d[key]]
print(d)
Output{'key1': ['abc', 'def', 'ghi'], 'key2': ['xyz', 'lmn']}
Explanation:
- Removing Digits with Regular Expressions code iterates through each list in the dictionary, applying re.sub(r'\d+', '', item) to remove all digits from each string using a regular expression (\d+ matches one or more digits).
- Updating the Dictionary updates strings with digits removed, replace the original strings in the dictionary for each key and final dictionary is printed without digits in any of the string values.
Using str.translate()
and str.maketrans()
This method uses str.translate() in combination with str.maketrans() to remove digits from each string.
Python
d = {'key1': ['abc1', 'def2', 'ghi3'], 'key2': ['xyz4', 'lmn5']}
# Create a translation table that maps digits (0-9) to None
r = str.maketrans('', '', '0123456789')
# Iterate through the dictionary to process each list of strings
for key in d:
# Use list comprehension to apply the translate() method to each string in the list
# The translate method removes all digits as specified in the translation table 'r'
d[key] = [item.translate(r) for item in d[key]]
print(d)
Output{'key1': ['abc', 'def', 'ghi'], 'key2': ['xyz', 'lmn']}
Explanation:
- Translation Table str.maketrans('', '', '0123456789') creates a table to remove digits from strings.
- Remove Digits translate() is applied to each string in the dictionary’s lists to remove the digits.
Using str.join()
This method iterates through each character in the string and removes the digits using a list comprehension.
Python
d = {'key1': ['abc1', 'def2', 'ghi3'], 'key2': ['xyz4', 'lmn5']}
# Iterate through the dictionary to process each list of strings
for key in d:
# Use list comprehension to process each string in the list
# For each string, iterate over its characters and keep only non-digit characters
d[key] = [''.join([char for char in item if not char.isdigit()]) for item in d[key]]
print(d)
Output{'key1': ['abc', 'def', 'ghi'], 'key2': ['xyz', 'lmn']}
Explanation:
- List comprehension filters out digits from each string in the dictionary’s lists.
- Dictionary is updated with the new lists of strings without digits
Using str.replace()
We can use str.replace() in a loop to replace each digit with an empty string.
Python
d = {'key1': ['abc1', 'def2', 'ghi3'], 'key2': ['xyz4', 'lmn5']}
# Iterate through the dictionary to process each list of strings
for key in d:
# Iterate over each string in the list, using enumerate to get the index and item
for idx, item in enumerate(d[key]):
# Use replace() to remove specific digits (1, 2, 3, 4, 5) from the string
d[key][idx] = item.replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', '')
print(d)
Output{'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.- Dictionary is updated with the modified strings, where the specified digits are removed from each string in the list.
Similar Reads
Python | Remove all digits from a list of strings The problem is about removing all numeric digits from each string in a given list of strings. We are provided with a list where each element is a string and the task is to remove any digits (0-9) from each string, leaving only the non-digit characters. In this article, we'll explore multiple methods
4 min read
Python Remove Item from Dictionary by Value We are given a dictionary and our task is to remove key-value pairs where the value matches a specified target. This can be done using various approaches, such as dictionary comprehension or iterating through the dictionary. For example: d = {"a": 10, "b": 20, "c": 10, "d": 30} and we have to remove
3 min read
Python - Remove Dictionary if Given Key's Value is N We are given a dictionary we need to remove key if the given value of key is N. For example, we are given a dictionary d = {'a': 1, 'b': 2, 'c': 3} we need to remove the key if the value is N so that the output becomes {'a': 1, 'c': 3}. We can use methods like del, pop and various other methods like
2 min read
Remove Spaces from Dictionary Keys - Python Sometimes, the keys in a dictionary may contain spaces, which can create issues while accessing or processing the data. For example, consider the dictionary d = {'first name': 'Nikki', 'last name': 'Smith'}. We may want to remove spaces from the keys to standardize the dictionary, resulting in {'fir
3 min read
Remove Dictionary from List If Key is Equal to Value in Python Removing dictionaries from a list based on a specific condition is a common task in Python, especially when working with data in list-of-dictionaries format. In this article, we will see various methods to Remove Dictionary from List If the Key is Equal to the Value in Python.Using filter()filter()
2 min read