Python | Remove all digits from a list of strings



Examples :

  Input:  [`alice1`,` bob2`, `cara3`]  Output:  [`alice`,` bob`, `cara`]  Input:  [` 4geeks`, `3for`,` 4geeks`]  Output:  [`geeks`,` for`, `geeks`] 

Method # 1: Python Regex

The Python regex pattern can also be used to find if each line is digit or not, and converting them to “”.

# Python program to remove all
# numbers from the string list

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 ))

Exit:

 [`geeks`,` for`, ` geeks`] 

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

# Python program to remove all
# numbers from the list of strings

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 ))

Exit:

 [`geeks `,` for`, `geeks`] 

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

# Python program to remove all
# numbers from the list of strings

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 ))

Exit :

 [`geeks`,` for`, `geeks`]