Given a Strings List, extract those with atleast one digit.
Input : test_list = [‘gf4g’, ‘is’, ‘best’, ‘gee1ks’]
Output : [‘gf4g’, ‘gee1ks’]
Explanation : 4, 1 are respective digits in string.
Input : test_list = [‘gf4g’, ‘is’, ‘best’, ‘geeks’]
Output : [‘gf4g’]
Explanation : 4 is digit in string.
Method #1 : Using list comprehension + any() + isdigit()
In this iteration for each string is done using list comprehension, any() and isdigit() is used for task of checking at least one digit.
step by step approach:
- Initialize the input list test_list with some string values.
- Print the original list.
- Create a list comprehension using sub as the loop variable to iterate over each element sub in test_list.
- For each element sub in test_list, use the any() function to check if any character in the string is a digit using the isdigit() method.
- If the above condition is true, append the element sub to the result list res.
- Print the list of strings with digits.
Python3
test_list = [ 'gf4g' , 'is' , 'best' , '4' , 'gee1ks' ]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if any (ele for ele in sub if ele.isdigit())]
print ( "Strings with any digit : " + str (res))
|
Output
The original list is : ['gf4g', 'is', 'best', '4', 'gee1ks']
Strings with any digit : ['gf4g', '4', 'gee1ks']
Time Complexity: O(n2)
Space Complexity: O(n)
Method #2 : Using any() + filter() + lambda
In this, we perform task of filtering using lambda and filter(), rest remains the same.
Python3
test_list = [ 'gf4g' , 'is' , 'best' , '4' , 'gee1ks' ]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda sub: any (
ele for ele in sub if ele.isdigit()), test_list))
print ( "Strings with any digit : " + str (res))
|
Output
The original list is : ['gf4g', 'is', 'best', '4', 'gee1ks']
Strings with any digit : ['gf4g', '4', 'gee1ks']
Time Complexity: O(n2)
Space Complexity: O(n)
Method #3 : Without any builtin methods
Python3
def fun(s):
digits = "0123456789"
x = s
for i in digits:
s = s.replace(i, "")
if ( len (x) ! = len (s)):
return True
return False
test_list = [ 'gf4g' , 'is' , 'best' , '4' , 'gee1ks' ]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
if fun(i):
res.append(i)
print ( "Strings with any digit : " + str (res))
|
Output
The original list is : ['gf4g', 'is', 'best', '4', 'gee1ks']
Strings with any digit : ['gf4g', '4', 'gee1ks']
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #4 : Using ord() function
Python3
test_list = [ 'gf4g' , 'is' , 'best' , '4' , 'gee1ks' ]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
for j in i:
if ( ord (j) > = ord ( '0' ) and ord (j) < = ord ( '9' )):
res.append(i)
break
print ( "Strings with any digit : " + str (res))
|
Output
The original list is : ['gf4g', 'is', 'best', '4', 'gee1ks']
Strings with any digit : ['gf4g', '4', 'gee1ks']
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Method 5 : Using regular expressions
In this approach, we use the re.search() function from the re module which searches for a pattern in the given string. The pattern ‘\d’ represents a digit in the string. The function returns a match object if there is a match and None otherwise. Hence, for every string in the list, we check if there is a digit present by calling re.search(‘\d’, i) where i is the current string in the list. If there is a match, we add the string i to the resultant list. Finally, the resultant list is returned which has all the strings containing at least one digit.
Python3
import re
test_list = [ 'gf4g' , 'is' , 'best' , '4' , 'gee1ks' ]
print ( "The original list is : " + str (test_list))
res = [i for i in test_list if re.search( '\d' , i)]
print ( "Strings with any digit : " + str (res))
|
Output
The original list is : ['gf4g', 'is', 'best', '4', 'gee1ks']
Strings with any digit : ['gf4g', '4', 'gee1ks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Apr, 2023
Like Article
Save Article