Python | Remove all digits from a list of strings
Last Updated :
26 Dec, 2024
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 to solve the problem.
Using Regular Expressions
Regular Expressions efficiently remove digits from strings by matching and replacing them. This uses the re.sub() function to substitute digits (\d) with an empty string. This method is very flexible for pattern matching and ideal for complex string manipulations.
Example:
Python
import re
s = ['Geeks123', 'for45', 'geeks678']
#Remove digit
res= [re.sub(r'\d+', '', i) for i in s]
print(res)
Output['Geeks', 'for', 'geeks']
Explanation:
- re.sub() Function: It replaces the matched digits with an empty string (''), effectively removing them.
- List Comprehension: The expression [re.sub(r'\d+', '', i) for i in s] loops through each element in the list s, applying the re.sub() function.
- Result: The output is a new list where the digits are removed from each string in s.
Let's discuss more method to remove all digits from a list of strings .
Using str.isdigit
str.isdigit checks if a character is a digit. This method iterates over each character in the string and checks if it's not a digit using char.isdigit(). Using list comprehension, this method removes all digits from each string while keeping the non-digit characters.
Example:
Python
s = ['Geeks123', 'for45', 'geeks678']
# Remove digit
res = [''.join([ch for ch in i if not ch.isdigit()]) for i in s]
print(res)
Output['Geeks', 'for', 'geeks']
Explanation:
- The expression [''.join([ch for ch in i if not ch.isdigit()]) for i in s] iterates over each string in the list s.
- Inside the list comprehension, ch for ch in i if not ch.isdigit() filters out digits from each string i. ch.isdigit() checks if a character is a digit.
- ''.join(): After filtering the characters, ''.join() combines the remaining characters (non-digits) back into a string.
Using str.translate
It is a fast method to remove specific characters, like digits from strings. It directly replaces the specified characters with an empty string. str.translate() removes characters by using a translation table, created with str.maketrans(), which maps digits to None.
Example:
Python
s= ['Geeks123', 'for45', 'geeks678']
res= [i.translate(str.maketrans('', '', '0123456789')) for i in s] # Remove digit
print(res)
Output['Geeks', 'for', 'geeks']
Explanation:
- str.maketrans(): This method creates a translation table that maps characters in the first string ('0123456789') to None, effectively removing any digits.
- translate(): The translate() method applies the translation table to each string in the list, removing the digits (since digits are mapped to None).
- [i.translate(str.maketrans('', '', '0123456789')) for i in s] loops through each string in the list s and removes any digits using the translation table.
Using filter
filter()
removes digits from strings in list, keeping only non-digit characters. It simplifies string cleaning in a functional way. This method might be less efficient compared to translate() for removing large sets of characters.
Example:
Python
s= ['Geeks123', 'for45', 'geeks678']
# Remove digit
res= [''.join(filter(lambda ch: not ch.isdigit(), i)) for i in s]
print(res)
Output['Geeks', 'for', 'geeks']
Explanation:
- List Comprehension: Iterates over each string i in the list s.
- filter(): Filters out digits using lambda ch: not ch.isdigit(), keeping only non-digit characters.
- ''.join(): Joins the non-digit characters back into a string.
Using str.replace
str.replace() in a loop remove digits from each string. It processes each digit (0-9) in every string, making it less efficient. This method is useful for small tasks or when working with small datasets where performance isn’t a concern.
Example:
Python
s = ['Geeks123', 'for45', 'geeks678']
for i in range(10):
s = [string.replace(str(i), '') for string in s] # Remove digit
print(s)
Output['Geeks123', 'for45', 'geeks678']
Explanation:
- The for digit in range(10) loop iterates over the digits from 0 to 9.
- replace(): Inside the list comprehension, string.replace(str(digit), '') removes the current digit (from 0 to 9) from each string in the list s.
Similar Reads
Python - Remove digits from Dictionary String Values List 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 {'key
3 min read
Python | Remove Kth character from strings list Sometimes, while working with data, we can have a problem in which we need to remove a particular column, i.e the Kth character from string list. String are immutable, hence removal just means re creating a string without the Kth character. Let's discuss certain ways in which this task can be perfor
7 min read
Python | Split strings and digits from string list Sometimes, while working with String list, we can have a problem in which we need to remove the surrounding stray characters or noise from list of digits. This can be in form of Currency prefix, signs of numbers etc. Let's discuss a way in which this task can be performed. Method #1 : Using list com
5 min read
Python | Remove given character from Strings list Sometimes, while working with Python list, we can have a problem in which we need to remove a particular character from each string from list. This kind of application can come in many domains. Let's discuss certain ways to solve this problem. Method #1 : Using replace() + enumerate() + loop This is
8 min read
Python - Remove String from String List This particular article is indeed a very useful one for Machine Learning enthusiast as it solves a good problem for them. In Machine Learning we generally encounter this issue of getting a particular string in huge amount of data and handling that sometimes becomes a tedious task. Lets discuss certa
4 min read