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 : ['4Lazyroar', '3for', '4Lazyroar'] Output : ['Lazyroar', 'for', 'Lazyroar']
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 = [ '4Lazyroar' , '3for' , '4Lazyroar' ] print (remove( list )) |
['Lazyroar', 'for', 'Lazyroar']
Time Complexity: O(n*m), where n is the number of strings in the list and m is the average length of each string. The program has to iterate over each string and remove any digits it finds in the string.
Auxiliary Space Complexity: O(1), as no additional data structure is used.
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 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 = [ '4Lazyroar' , '3for' , '4Lazyroar' ] print (remove( list )) |
['Lazyroar', 'for', 'Lazyroar']
Time complexity: O(n), where n is the number of strings in the list.
Auxiliary space: O(n), where n is the number of strings in the list
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 = [ '4Lazyroar' , '3for' , '4Lazyroar' ] print (remove( list )) |
['Lazyroar', 'for', 'Lazyroar']
Time Complexity: O(n*m), where n is the number of strings in the list and m is the average length of each string. The program has to iterate over each string and remove any digits it finds in the string.
Auxiliary Space: O(1), as no additional data structure is used.
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 = [ '4Lazyroar' , '3for' , '4Lazyroar' ] x = [] for i in list : x.append(remove(i)) print (x) |
['Lazyroar', 'for', 'Lazyroar']
Method #5 : Using replace() method
Python3
# Python program to Remove all # digits from a list of string def remove(s): digits = "0123456789" for i in digits: s = s.replace(i, "") return s # Driver code res = [] list = [ '4Lazyroar' , '3for' , '4Lazyroar' ] for i in list : res.append(remove(i)) print (res) |
['Lazyroar', 'for', 'Lazyroar']
Method #6: Using filter() function
One approach you could use to remove all digits from a list of strings is to use a combination of the join() and filter() functions. Here’s an example of how this could be done:
Python3
def remove_digits(strings): def is_alpha(char): return char.isalpha() return [''.join( filter (is_alpha, string)) for string in strings] strings = [ 'alice1' , 'bob2' , 'cara3' ] print (remove_digits(strings)) # ['alice', 'bob', 'cara'] #This code is contributed by Edula Vinay Kumar Reddy |
['alice', 'bob', 'cara']
This approach first defines a helper function is_alpha() that returns True if a character is an alphabetical character, and False otherwise. Then, it uses a list comprehension to iterate through each string in the input list, and filters out all non-alphabetic characters using filter() and is_alpha(). Finally, it uses join() to join all of the remaining characters in each string back together, resulting in a list of strings with all digits removed.
The time complexity of the approach I provided using join() and filter() is O(n*m), where n is the length of the input list and m is the average length of the strings in the list. This is because the list comprehension iterates through each string in the input list, and the filter() function iterates through each character in each string.
The space complexity is also O(n*m), as the list comprehension creates a new list of strings with the same length as the input list, and each string in the new list has the same length as the corresponding string in the input list.
Method #7: Using reduce() function and filter() function.
Algorithm:
- Define a function named “remove_digits” that takes a list of strings as input.
- Using a list comprehension, iterate over the strings in the input list.
- For each string, use filter() to remove all the digits from the string.
- Join the filtered characters into a string using reduce() with a lambda function that concatenates two strings.
- Return the modified list of strings.
- Call the function with a sample list and print the result.
Python3
# Python program to Remove all # digits from a list of string from functools import reduce def remove_digits(lst): return [ reduce ( lambda x, y: x + y, filter ( lambda x: not x.isdigit(), s), '') for s in lst] list = [ '4Lazyroar' , '3for' , '4Lazyroar' ] res = remove_digits( list ) print (res) |
['Lazyroar', 'for', 'Lazyroar']
Time Complexity: O(nm), where n is the number of strings in the input list and m is the length of the longest string in the list. The time complexity of filter() and reduce() functions is O(n), and we apply them to each string in the list, so the overall time complexity is O(nm).
Auxiliary Space: O(nm), where n is the number of strings in the input list and m is the length of the longest string in the list. We create a new string for each string in the list, so the overall space complexity is O(nm).