Skip to content
Related Articles

Related Articles

Python | Remove all digits from a list of strings

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 16 Aug, 2022
View Discussion
Improve Article
Save Article

Given a list of strings, write a Python program to remove all digits from the list of string. 

Examples:

Input : ['alice1', 'bob2', 'cara3']
Output : ['alice', 'bob', 'cara']

Input : ['4geeks', '3for', '4geeks']
Output : ['geeks', 'for', 'geeks']

Method #1: Python Regex Python regex pattern can also be used to find if each string contains a digit or not and converting them to “”. 

Python3




# Python program to Remove all
# digits from a list of string
import re
 
def remove(list):
    pattern = '[0-9]'
    list = [re.sub(pattern, '', i) for i in list]
    return list
 
# Driver code
 
list = ['4geeks', '3for', '4geeks']
print(remove(list))

Output:

['geeks', 'for', 'geeks']

Method #2: Using str.maketrans() method The maketrans() method returns a translation table that maps each character in the into string into the character at the same position in the outtab string. In this particular problem we translate each each digit to “” using for loop. 

Python3




# Python program to Remove all
# digits from a list of string
from string import digits
 
def remove(list):
    remove_digits = str.maketrans('', '', digits)
    list = [i.translate(remove_digits) for i in list]
    return list
 
# Driver code
 
list = ['4geeks', '3for', '4geeks']
print(remove(list))

Output:

['geeks', 'for', 'geeks']

Method #3: Using str.isalpha() method In this approach we use two for loops and check if the character of string is an alphabet or not. If yes, join it within the list, otherwise leave it. 

Python3




# Python program to Remove all
# digits from a list of string
from string import digits
 
def remove(list):
    list = [''.join(x for x in i if x.isalpha()) for i in list]
             
    return list
 
# Driver code
 
list = ['4geeks', '3for', '4geeks']
print(remove(list))

Output:

['geeks', 'for', 'geeks']

Method #4: Using ord() function

Python3




# Python program to Remove all
# digits from a list of string
def remove(s):
    ns=""
    for i in s:
        if(not ord(i) in range(48,58)):
            ns+=i
    return ns   
 
list = ['4geeks', '3for', '4geeks']
x=[]
for i in list:
    x.append(remove(i))
print(x)

Output

['geeks', 'for', 'geeks']

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!