Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Remove all digits from a list of strings using Python?
When working with lists of strings in Python, you might need to remove all numeric digits from each string. This is a common data cleaning task in text processing. Python provides several approaches to accomplish this: using string methods, regular expressions, or character filtering.
Using replace() Method
The simplest approach is to use the replace() method to remove each digit individually ?
def remove_digits(string_list):
return [s.replace('0', '').replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', '').replace('6', '').replace('7', '').replace('8', '').replace('9', '') for s in string_list]
# List of strings containing digits
data = ['John53mass', '66elsa98Marvel', '300perfect04stay']
result = remove_digits(data)
print("After removing digits:", result)
After removing digits: ['Johnmass', 'elsaMarvel', 'perfectstay']
Using Regular Expressions (re module)
The re module provides a more elegant solution using pattern matching ?
import re
def remove_digits_re(string_list):
return [re.sub(r'\d+', '', s) for s in string_list]
# List of strings containing digits
data = ['John53mass', '66elsa98Marvel', '300perfect04stay']
result = remove_digits_re(data)
print("After removing digits:", result)
After removing digits: ['Johnmass', 'elsaMarvel', 'perfectstay']
Using isalpha() Method
Filter characters by keeping only alphabetic characters using isalpha() ?
def remove_digits_alpha(string_list):
return [''.join(char for char in s if char.isalpha()) for s in string_list]
# List of strings containing digits
data = ['John53mass', '66elsa98Marvel', '300perfect04stay']
result = remove_digits_alpha(data)
print("After removing digits:", result)
After removing digits: ['Johnmass', 'elsaMarvel', 'perfectstay']
Using translate() Method
The translate() method with str.maketrans() offers another efficient approach ?
def remove_digits_translate(string_list):
translator = str.maketrans('', '', '0123456789')
return [s.translate(translator) for s in string_list]
# List of strings containing digits
data = ['John53mass', '66elsa98Marvel', '300perfect04stay']
result = remove_digits_translate(data)
print("After removing digits:", result)
After removing digits: ['Johnmass', 'elsaMarvel', 'perfectstay']
Comparison
| Method | Performance | Readability | Best For |
|---|---|---|---|
replace() |
Slow | Low | Simple cases |
re.sub() |
Good | High | Complex patterns |
isalpha() |
Good | High | Character filtering |
translate() |
Fast | Medium | Large datasets |
Conclusion
For most cases, use re.sub() for its clarity and flexibility. Use translate() for performance-critical applications with large datasets. The isalpha() method is ideal when you need to preserve only alphabetic characters.
