Python Program to check whether all elements in a string list are numeric
Last Updated :
22 Apr, 2023
Given a list that contains only string elements the task here is to write a Python program to check if all of them are numeric or not. If all are numeric return True otherwise, return False.
Input : test_list = ["434", "823", "98", "74"]
Output : True
Explanation : All Strings are digits.
Input : test_list = ["434", "82e", "98", "74"]
Output : False
Explanation : e is not digit, hence verdict is False.
Method 1 : Using all(), isdigit() and generator expression
In this, we check for numbers from isdigit(). all() is used to check for all strings to be number, and iteration for each string is done using generator expression.
Example:
Python3
test_list = [ "434" , "823" , "98" , "74" ]
print ( "The original list is : " + str (test_list))
res = all (ele.isdigit() for ele in test_list)
print ( "Are all strings digits ? : " + str (res))
|
Output
The original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using all(), isdigit() and map()
In this, we extend test logic to each string using map(), rather than generator expression. Rest all the functionalities are performed similarly to the above method.
Example:
Python3
test_list = [ "434" , "823" , "98" , "74" ]
print ( "The original list is : " + str (test_list))
res = all ( map ( str .isdigit, test_list))
print ( "Are all strings digits ? : " + str (res))
|
Output
The original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3 : Using isnumeric() and len() methods
Python3
test_list = [ "434" , "82e" , "98" , "74" ]
print ( "The original list is : " + str (test_list))
c = 0
for i in test_list:
if i.isnumeric():
c + = 1
res = False
if (c = = len (test_list)):
res = True
print ( "Are all strings digits ? : " + str (res))
|
Output
The original list is : ['434', '82e', '98', '74']
Are all strings digits ? : False
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4 : Using replace() and len() methods
Python3
test_list = [ "434" , "82e" , "98" , "74" ]
print ( "The original list is : " + str (test_list))
c = 0
numbers = "0123456789"
for i in test_list:
for j in numbers:
i = i.replace(j, "")
if ( len (i) = = 0 ):
c + = 1
res = False
if (c = = len (test_list)):
res = True
print ( "Are all strings digits ? : " + str (res))
|
Output
The original list is : ['434', '82e', '98', '74']
Are all strings digits ? : False
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method 5 : Using filter()+list()+lambda functions
Python3
test_list = [ "434" , "823" , "98" , "74" ]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda x: x.isdigit(), test_list))
if ( len (res) = = len (test_list)):
res = True
else :
res = False
print ( "Are all strings digits ? : " + str (res))
|
Output
The original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 6 : Using reduce()
Python3
from functools import reduce
from operator import and_
test_list = [ "434" , "823" , "98" , "74" ]
print ( "The original list is : " + str (test_list))
res = reduce (and_, (string.isdigit() for string in test_list))
print ( "Are all strings digits ? : " + str (res))
|
Output
The original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 7:Using for loop
Python3
test_list = [ "434" , "823" , "98" , "74" ]
print ( "The original list is : " + str (test_list))
for string in test_list:
if not string.isdigit():
res = False
break
else :
res = True
print ( "Are all strings digits ? : " + str (res))
|
Output
The original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time complexity: O(n)
Auxiliary Space: O(1)
Method 8: Using loop +try and except method.
Python3
test_list = [ "434" , "823" , "98" , "74" ]
print ( "The original list is : " + str (test_list))
for i in test_list:
try :
int (i)
res = True
except :
res = False
print ( "Are all strings digits ? : " + str (res))
|
Output
The original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time complexity: O(n)
Auxiliary Space: O(1)
Method 9: Using re.match() method
Approach
- Initiate a for loop to traverse test_list, variable c=0
- Check whether each string is numeric or not using regular expression ^[0-9]+$ and method re.match() if yes increment c by 1
- If c equals to length of test_list then all strings have digits, set res to True or else False
- Display res
Python3
import re
test_list = [ "434" , "823" , "98" , "74" ]
print ( "The original list is : " + str (test_list))
c = 0
for i in test_list:
if re.match( '^[0-9]+$' , i):
c + = 1
res = c = = len (test_list)
print ( "Are all strings digits ? : " + str (res))
|
Output
The original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time Complexity: O(N) N – length of test_list
Auxiliary Space: O(1) because we used single variable res to store result
Share your thoughts in the comments
Please Login to comment...