How to Remove all digits from a list of strings using Python?


The list data structure holds elements of different datatypes like integers, strings, or float numbers. The elements that are once defined inside the list data structure that is within the square brackets cannot be changed. The digits which are defined inside the string value can be removed and later returns only the string value without digits. In this article, the digits are removed from every string defined in the list data structure and also provided with string manipulation techniques.

Remove all digits from a list of strings

The integers are removed from the string and the process is mentioned from the simple operations to the complex functionalities. The strings are composed of characters and print only strings without integers involve some complex process.

Approach

  • Approach 1 − Using the replace module

  • Approach 2 − Using the re module

  • Approach 3 − Using the isalpha() method

Approach 1: Python Program to remove all digits from a list of strings using replace module

The replace module is used to replace the integers with spaces in the declared variables.

Algorithm

  • Step 1 − The function is defined with one input string argument.

  • Step 2 − Using the list comprehension, to replace each element.

  • Step 3 − The input is initialized with a string variable and assigned to string1 variable along with the integers

  • Step 4 − The output of the list is printed without the integers

Example

#defining the remove integer function with one argument 
def removeinteger(stringlist):
	return [a.replace('0', '').replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', ''). replace('6', ''). replace('7', ''). replace('8', ''). replace('9', '') for a in stringlist]
#initializing the list of strings
string1 = ['John53mass', '66elsa98Marvel', '300perfect04stay']
#the newlist holds the string after removing the integers
newlist = removeinteger(string1)
#Print the function 
print("The list of string after integer removal:", newlist)

Output

The list of string after integer removal: ['Johnmass', 'elsaMarvel', 'perfectstay']

Approach 2: Python Program to remove all digits from a list of strings using re module

The re module is used to remove all the integers in the given list of strings and later replace all digits into strings.

Algorithm

  • Step 1 − The input variable is declared with the characters and integers together like John53mass.

  • Step 2 − The program works by removing only that number (53) from the string.

  • Step 3 − The function named removeinteger() is defined with a single argument and re module is imported to use some inbuilt functions.

  • Step 4 − The function re.sub() from the re module is used to find the strings and then replace them with integer values.

  • Step 5 − To hold the result, the newlist is created.

  • Step 6 − The for loop will iterate through the list based on the length of the list.

  • Step 7 − Finally, the output is printed.

Example

#importing the required module
import re
#function defined with one argument
def removeint(val):
	return re.sub("\d+", "", val)
#initializing the list of strings with numbers inside string
string1 = ['John53mass', '66elsa98Marvel', '300perfect04stay']
#initializing the empty list
newlist = []
#for loop is used to iterate through the list along with the len function
for a in range(len(string1)):
	newlist.append(removeint(string1[a]))
#returning the list after removing the integers
print("The list of string after integer removal:", newlist)

Output

The list of string after integer removal: ['Johnmass', 'elsaMarvel', 'perfectstay']

Approach 3: Python Program to remove all digits from a list of strings using isalpha() method

The list will be printed after removing the integers from the list of elements, using the isalpha() method.

Algorithm

  • Step 1 − The input can be any string along with numbers and the output is printed without that numbers

  • Step 2 − An empty list is declared and assigned to empty variable.

  • Step 3 − Using list comprehension, we can iterate through the elements depending on its length of the given list.

  • Step 4 − The alphabets are appended using the join function and then stored in empty list.

  • Step 5 − Final list is printed without numbers in string

Example

#initializing the list data structure with string and integer elements along with integer values
string1 = ['John53mass', '66elsa98Marvel', '300perfect04stay']
#declaring an empty list
empty = []
#for loop is used to iterate through the list by ranging based on the length
for a in range(len(string1)):
	empty.append(''.join(e for e in string1[a] if e.isalpha()))
#printing the list after removing the integers from the list 
print("The list of string after integer removal:", empty)

Output

The list of string after integer removal: ['Johnmass', 'elsaMarvel', 'perfectstay']

Conclusion

Python is used in a wide range of technologies like machine learning and artificial intelligence. The approaches help the programmer to remove the integer from the list of strings with the help of modules and list comprehension. Data handling is the most prominent one in this current technology and removing the integer from the declared input string is one of the fundamental functions.

Updated on: 04-Sep-2023

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements