Open In App

Python | Remove all digits from a list of strings

Last Updated : 26 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article

Similar Reads